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

[玩转系统] 使用 PowerShell 将是/否(复选框)字段添加到 SharePoint 列表

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

使用 PowerShell 将是/否(复选框)字段添加到 SharePoint 列表


要求: 将“是/否”(复选框)字段添加到 SharePoint 列表。

如何在 SharePoint 列表中创建是/否复选框列?

请按照以下步骤将“是/否”字段添加到 SharePoint 列表:

  1. 浏览您的 SharePoint 网站并导航到要添加“是/否”列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,并将类型指定为“是/否复选框”。
  4. 填写其他可选值,例如字段描述、默认值,然后单击“确定”在 SharePoint 列表中创建是/否复选框字段。

    [玩转系统] 使用 PowerShell 将是/否(复选框)字段添加到 SharePoint 列表

SharePoint PowerShell 在列表或库中创建是/否字段:

使用此 PowerShell 脚本将“是/否”列添加到 SharePoint 列表。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to add Field to list
Function Add-YesNoFieldToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $FieldName,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description,
        [Parameter(Mandatory=$false)] [string] $DefaultValue = "0"
    )
 
    #Generate new GUID for Field ID
    $FieldID = [guid]::newguid()
 
    Try {
        #Get Web List and Objects
        $Web = Get-SPWeb $SiteURL
        $List = $Web.Lists[$ListName]
 
        #Check if the Field exists in list already
        $Fields = $List.Fields
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Field '$FieldName' already exists in the List!" -f Yellow
        }
        else
        {
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='Boolean' ID='{$FieldID}' DisplayName='$DisplayName' Name='$FieldName' Description='$Description'><Default>$DefaultValue</Default></Field>"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
 
            Write-host "New Field Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Field to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://intranet.crescent.com/sites/projects"
$ListName="Projects"
$FieldName="IsActive"
$DisplayName="Is Active"
$Description="Specify if the Project is Active"
$DefaultValue="1" #0 for No / 1 for Yes
 
#Call the function to add field to list
Add-YesNoFieldToList -SiteURL $SiteURL -ListName $ListName -FieldName $FieldName -DisplayName $DisplayName -Description $Description -DefaultValue $DefaultValue 

字段架构支持更多属性,例如 Group、Indexed、ShowInNewForm 等。有关完整字段架构,请参阅此 MSDN 链接:https://docs.microsoft.com/en-us/sharepoint/dev/schema/field-element -场地

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

取消回复欢迎 发表评论:

关灯