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

[玩转系统] SharePoint Online:使用 PowerShell 在列表中创建单行文本列

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

SharePoint Online:使用 PowerShell 在列表中创建单行文本列


要求:PowerShell 将单行文本列添加到 SharePoint Online 列表。

如何将单行文本列添加到 SharePoint Online 列表?

SharePoint Online 中的“单行文本”列在单行中存储最多 255 个字符的文本值。当您想要添加自由流动的文本值作为列表或文档库的元数据时,我们可以使用此字段类型。要将单行文本字段添加到列表中,请执行以下操作:

  1. 浏览到 SharePoint Online 网站并导航到要在其中创建新的单行文本列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,将类型指定为“单行文本”。
  4. 填写其他可选值,然后单击“确定”以在 SharePoint Online 列表中创建单行文本列。

    [玩转系统] SharePoint Online:使用 PowerShell 在列表中创建单行文本列

PowerShell 将单行文本列添加到 SharePoint Online 中的列表

以下是 PowerShell 脚本,用于为 SharePoint Online 中的任何现有列表或库创建单行文本列:


#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"

#Custom function to add column to list
Function Add-SingleLineTextColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$true)] [string] $Description,
        [Parameter(Mandatory=$true)] [string] $IsRequired,        
        [Parameter(Mandatory=$true)] [string] $EnforceUniqueValues,
        [Parameter(Mandatory=$true)] [string] $MaxLength
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    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()

        #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
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$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"

#Call the function to add column to list
Add-SingleLineTextColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -IsRequired $IsRequired -MaxLength $MaxLength -EnforceUniqueValues $EnforceUniqueValues 

如何设置字段的默认值?

您可以通过将 标记添加到字段架构来设置默认值。这是一个示例:


$FieldSchema = "<Field Type='Text' Name='$Name' StaticName='$Name' DisplayName='$DisplayName'><Default>Project Code - XXXXX</Default></Field>"

PnP PowerShell 将字段添加到 SharePoint Online 列表

以下是将单行文本类型的列表列添加到 SharePoint Online 列表的 PowerShell:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Team Projects"
$FieldTitle= "Project Status"
$FieldInternalName ="ProjectStatus"
$FieldType = "Text"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #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
}

同样,我们也可以从 XML 架构向 SharePoint Online 列表添加文本列。


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

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='FALSE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"

#Add Text Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

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

取消回复欢迎 发表评论:

关灯