当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 添加新列表项

作者:精品下载站 日期:2024-12-14 13:49:44 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 添加新列表项


要求:使用 SharePoint Online 时,我需要通过 PowerShell 脚本将新列表项添加到列表中。本博文将介绍如何使用 PowerShell 在 SharePoint Online 中创建列表项,并提供一些示例。

如何将项目添加到 SharePoint Online 中的列表?

将项目添加到 SharePoint 列表是一项常见任务,您可能会发现自己经常这样做。以下是如何操作的快速概述:

  1. 登录到您的 SharePoint 网站并导航到要添加项目的列表。
  2. 单击命令栏中的“+ 新建”链接。
  3. 在相应字段中输入新项目的信息。
  4. 单击“保存”按钮。

    [玩转系统] SharePoint Online:使用 PowerShell 添加新列表项

这就是全部!让我们看看如何使用 PowerShell 在 SharePoint 在线列表中添加项目。

SharePoint Online:用于添加列表项的 PowerShell

在许多情况下,您可能希望在 SharePoint Online 中创建列表项,而不需要通过 SharePoint Online 中基于 Web 的用户界面。幸运的是,我们有一个简单的方法可以使用 PowerShell 来完成此操作。在这篇博文中,我们将向您展示如何使用 PowerShell 将项目添加到 SharePoint Online 列表。

要使用 PowerShell 添加列表项,我们必须为列表中的每个自定义列设置值。以下是用于创建列表项的 SharePoint Online PowerShell:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Config Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Tasks"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
 
    #sharepoint online powershell add list item
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation  
    $ListItem = $List.AddItem($ListItemInfo)
    
    #Set Column Values
    $ListItem["Title"] = "Project Darwin"

    #Set People Picker Field value
    $AssignedToUser = $Ctx.web.EnsureUser("[email protected]")
    $ListItem["AssignedTo"] = $AssignedToUser
    
    #Set Date Fields
    $ListItem["StartDate"] = "01/07/2015"
    $ListItem["DueDate"] = "01/08/2015"
    
    #Set Percentage Field 
    $ListItem["PercentComplete"] = "0.2" #20%

    #add item to sharepoint online list powershell
    $ListItem.Update()
    $Ctx.ExecuteQuery()
 
    Write-host -f Green "New Item has been added to the List!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}  

这会将新项目插入任务列表中。这是一种使用 PowerShell 将项目添加到 SharePoint Online 列表的快速而简单的方法,而无需使用 Web 浏览器用户界面。结果如下:

[玩转系统] SharePoint Online:使用 PowerShell 添加新列表项

SharePoint Online PowerShell 创建列表项

让我们添加一些错误处理代码并在脚本中运行时获取凭据。以下是用于添加列表项的 SharePoint Online PowerShell CSOM:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Random"
$ItemsToCreate="1000"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
        
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #add an item to list SharePoint Online PowerShell - with Random Data
    For($i=1; $i -le $ItemsToCreate; $i++)
    {
        #create list item in sharepoint online using powershell
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $I
        $ListItem.Update()
        $Ctx.ExecuteQuery()

        Write-host "New Item $I Added to the List!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message
}

该脚本随机批量创建列表项。

PnP PowerShell 在 SharePoint Online 中添加列表项

使用 SharePoint Online 时,您可能需要以编程方式将项目添加到列表中。幸运的是,有一种简单的方法可以使用 PowerShell 来完成此任务。您可以使用 PowerShell 脚本来帮助自动创建需要定期添加或无人值守的内容。

下面介绍了如何使用 PnP PowerShell cmdlet Add-PnPListItem 将项目添加到 SharePoint Online 中的列表。

  1. 第一步是连接到您的 SharePoint Online 网站 URL。您可以通过运行以下 cmdlet 来执行此操作:Connect-PnPOnline。
  2. 连接后,您可以使用 Add-PnPListItem cmdlet 将项目添加到列表中。例如,以下命令将向列表添加一个新项目,其中包含字段“内部名称”和该字段的值: Add-PnPlistItem -list “列表名称” -values @{“字段内部名称”=“字段值”; “字段内部名称”=“字段值”}

这是一个例子:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Add List Item - Internal Names of the columns: Value
Add-PnPListItem -List $ListName -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}

如果您想添加特定内容类型的项目,您还可以指定带有名称或 ID 的“ContentType”参数。执行此 PowerShell 脚本后,一个新项目将添加到列表中。使用 PowerShell 将项目添加到 SharePoint Online 列表是一种快速而简单的填充数据的方法。

如何设置托管元数据、超链接、查找、人员或组等字段?

上述脚本适用于简单的字段类型,例如单行文本、选择、是/否等。但是,我们必须针对其他字段类型(例如托管元数据、超链接、查找、人员选择器等)以不同的方式处理它们以下是我的帖子可以提供帮助:

  • SharePoint Online:PowerShell 更新查找字段值
  • SharePoint Online:使用 PowerShell 更新超链接字段值
  • SharePoint Online:PowerShell 更新查找字段值
  • SharePoint Online:使用 PowerShell 更新托管元数据字段值
  • SharePoint Online:使用 PowerShell 更新人员或组字段值
  • SharePoint Online:使用 PowerShell 更新选择字段值
  • SharePoint Online:使用 PowerShell 更新“是/否(复选框)”字段值
  • SharePoint Online:PowerShell 更新日期字段值

经常问的问题:

如何在 SharePoint Online 中批量添加多个列表项?

您可以使用 PnP PowerShell 中的 New-PnPBatch cmdlet 更快地执行批量操作,例如删除或将项目批量添加到 SharePoint Online 列表。
更多信息:如何使用PowerShell批量添加多个列表项?

如何将数据从 CSV 导入到 SharePoint Online 列表?

虽然您可以从 CSV 文件或 Excel 创建列表,但可以使用 PowerShell 中的 Import-CSV 和 Add-PnPList 项 cmdlet 来实现连续导入。
详细信息:将 CSV 导入到 SharePoint 在线列表 PowerShell

如何将文件上传到 SharePoint Online PowerShell?

要使用 PowerShell 将文件上传到 SharePoint 文档库,请使用 CSOM 中的 Files.Add 方法或 PnP PowerShell 中的 Add-PnPFile cmdlet。
详细信息:使用 PowerShell 将文件上传到 SharePoint Online

如何在 PowerShell 中从 SharePoint Online 列表获取项目?

使用 PnP PowerShell 中的“List.GetItems”方法或 Get-PnPListItem 从 SharePoint Online 查询和获取列表项。
详细信息:SharePoint Online 获取列表项 PowerShell

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯