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

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

作者:精品下载站 日期:2024-12-14 14:07:58 浏览:14 分类:玩电脑

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


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

用于将列添加到 SharePoint 列表的 PowerShell 脚本:

您是否需要向 SharePoint 列表添加列,但不想手动执行此操作?在这篇博文中,我将引导您完成使用 PowerShell 将列添加到列表的步骤。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired)
{
    #Set the Error Action
    $ErrorActionPreference = "Stop"

    Try{
        #Get the List
        $List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
        
        #Check if List with specific name exists
        if($List -ne $null)
        {
            if(!$List.Fields.ContainsField($FieldName))
            {      
                #Add columns to the List 
                $List.Fields.Add($FieldName,$FieldType,$IsRequired)

                #Update the List
                $List.Update()

                #Update the default view to include the new column
                $View = $List.DefaultView # OR $List.Views["All Items"]
                $View.ViewFields.Add($FieldName)
                $View.Update()

                write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
            }
            else
            {
                write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
            }
        }
        else
        {
            write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
        }        
    }
     catch {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    finally {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    }
}

#Parameters 
$SiteURL="https://intranet.crescent.com/"
$ListName = "Customer Directory"

#Add 'Name' - Single Line of Text Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Text
$FieldName="Name"
$IsRequired = $False
#Call the function to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Add Phone Number - Number Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Number
$FieldName="Phone Number"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Date of Joing - Date Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::DateTime
$FieldName="Date of Join"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

您还可以将字段从字段架构添加到 SharePoint 列表。具体方法如下:


#Field Schemas
$NameFldSchema="<Field Type='Text' DisplayName='Name' Required='False' MaxLength='255' StaticName='Name' Name='Name' />"
$PhoneNoFldSchema="<Field Type='Number' DisplayName='Phone Number' Required='False' MaxLength='255' StaticName='PhoneNumber' Name='PhoneNumber' />"
$DOBFldSchema="<Field Type='DateTime' DisplayName='Date of Birth' Required='False' MaxLength='255' StaticName='DateOfBirth' Name='DateOfBirth' /> "
#For Field schemas, Refer: https://msdn.microsoft.com/en-us/library/office/aa979575(v=office.15).aspx
  
#Add Columns to the List
$List.Fields.AddFieldAsXml($NameFldSchema, $True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

虽然上面的脚本相当简单,但要添加其他类型的字段(例如选择字段、查找字段等),请参阅以下帖子:

  • 如何使用 PowerShell 将选择字段添加到 SharePoint 列表?
  • PowerShell 创建新的单多行文本字段到 SharePoint 列表
  • 使用 PowerShell 将日期时间列添加到 SharePoint 列表
  • 如何使用 PowerShell 将查找字段添加到 SharePoint 列表?
  • 如何使用 PowerShell 将托管元数据列添加到 SharePoint 列表?

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

取消回复欢迎 发表评论:

关灯