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

[玩转系统] 如何使用 PowerShell 将列添加到 SharePoint Online 中的列表?

作者:精品下载站 日期:2024-12-15 00:27:29 浏览:13 分类:玩电脑

如何使用 PowerShell 将列添加到 SharePoint Online 中的列表?


要求:使用 PowerShell 将列添加到 SharePoint Online 中的列表。

如何将列添加到 SharePoint Online 列表或库?

SharePoint Online 列表用于存储数据,每个列表可以有多个列。列定义可以存储在列表中的数据类型。例如,员工列表可能包含其姓名、部门和职位的列。将列添加到 SharePoint Online 中的列表是一个简单的过程。

以下是有关如何使用 Web 界面将新列添加到 SharePoint Online 列表的分步指南:

  1. 导航到 SharePoint Online 网站,然后转到要添加新列的所需列表。您可以通过单击网站导航中的名称或转到网站内容并选择列表来访问该列表。
  2. 进入列表视图后,单击位于列表视图标题中的“+ 添加列”按钮。

    [玩转系统] 如何使用 PowerShell 将列添加到 SharePoint Online 中的列表?

  3. 将出现一个弹出页面,提供多种列类型可供选择,例如:

    [玩转系统] 如何使用 PowerShell 将列添加到 SharePoint Online 中的列表?

    • 单行文本
  4. 多行文本
  5. 选择(下拉菜单)
  6. 数字
  7. 货币
  8. 日期和时间
  9. 查找(参考另一个列表)
  10. 是/否(复选框)
  11. 个人或团体
  12. 超链接或图片
  13. 选择所需的列类型,屏幕右侧将出现一个新的配置面板。
  14. 在配置面板中,提供以下信息:

    • 名称:输入新列的名称。
  15. 说明:(可选)提供说明以帮助用户了解该列的用途。
  16. 特定于类型的设置:根据您选择的列类型,可能还有其他可用的配置选项,例如下拉菜单、数字格式或日期和时间格式的选项。
  17. 配置新列后,单击配置面板底部的“保存”按钮。

    [玩转系统] 如何使用 PowerShell 将列添加到 SharePoint Online 中的列表?

  18. 新列现在将出现在列表视图中,您可以开始使用它来存储和显示附加信息。

就是这样!使用 Web 界面,您已成功将新列添加到 SharePoint Online 列表。请记住,如果需要,可以稍后通过转到列表设置页面并选择列的名称来调整列的设置。

使用 PowerShell CSOM 脚本将列添加到列表

借助 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 parameters
$SiteURL="https://salaudeen.sharepoint.com/sites/Retail"
$ListName="Projects"
$Name="ProjectCode"
$DisplayName="Project Code"
$Description="Enter the Project Code"
$IsRequired = "TRUE"
$EnforceUniqueValues="FALSE" #Must set Indexed="FALSE", if Unique="TRUE"
$MaxLength="100"
 
#Generate new GUID for Field ID
$FieldID = New-Guid
 
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)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Check if the column exists in list already
    $Fields = $List.Fields
    $Ctx.Load($Fields)
    $Ctx.executeQuery()
    $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
    if($NewField -ne $NULL)  
    {
        Write-host "Column $Name already exists in the List!" -f Yellow
    }
    else
    {
        #Define XML for Field Schema
        $FieldSchema = "<Field Type='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
        $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
        $Ctx.ExecuteQuery()    
 
        Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Adding Column to List!" $_.Exception.Message
}

如果您需要添加多个字段或者想要自动执行将字段添加到列表的过程,这会特别有用。

PnP PowerShell 将列添加到 SharePoint Online 列表

如果您希望在 SharePoint Online 中以编程方式将列添加到列表中,请使用此 PnP PowerShell 脚本。在运行此脚本之前,请确保您的计算机上安装了 PnP.PowerShel 模块。使用 Add-PnPField cmdlet 将新列添加到 SharePoint Online 列表。此 cmdlet 要求您指定多个参数,例如列名称、列类型和显示名称。


#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Projects"
$ListName= "Team Projects"
$FieldTitle= "Project ID"
$FieldInternalName ="ProjectID"
$FieldType = "Text"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    #Add new field to list
    Add-PnPField -List $ListName -DisplayName $FieldTitle -InternalName $FieldInternalName -Type $FieldType -Required -AddToDefaultView -ErrorAction Stop
    Write-host -f Green "New Field '$FieldTitle' Added to the List!" 
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

确保根据您的要求替换参数。同样,您可以按其架构将字段添加到 SharePoint Online 列表:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$ListName= "Projects"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='TRUE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"
 
#Add a Field to list from XML schema
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

请参阅此 Microsoft 文章了解所有可用的字段架构:https://learn.microsoft.com/en-us/sharepoint/dev/schema/field-element-field

添加其他字段类型怎么样?

以下是我关于使用 PowerShell 将其他字段类型添加到 SharePoint Online 列表的帖子列表:

  • 将单行文本列添加到列表
  • 将计算列添加到列表
  • 将查找列添加到列表
  • 将选择字段添加到列表
  • 将日期和时间列添加到列表
  • 添加人员或组(人员选择器)列
  • 将多行文本列添加到列表
  • 将“是/否”复选框列添加到列表中
  • 将超链接或图片列添加到列表
  • 将托管元数据列添加到列表
  • 将货币列添加到列表
  • 将数字列添加到列表

包起来

在本文中,我们演练了使用 PowerShell 将列添加到 SharePoint Online 列表的过程。通过利用 SharePoint Online CSOM 和 PnP PowerShell 的强大功能,您可以自动化并简化添加 SharePoint Online 列表列的过程。总之,使用 PowerShell 将列添加到 SharePoint Online 中的列表是一个简单的过程,可以节省时间并减少工作流中的错误。

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

取消回复欢迎 发表评论:

关灯