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

[玩转系统] SharePoint Online:使用 PowerShell 将多行文本列添加到列表

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

SharePoint Online:使用 PowerShell 将多行文本列添加到列表


SharePoint Online 中的多行文本列用于存储多行文本值。根据字段设置,它可以包含富文本,包括文本、图像、HTML 格式的值等。在本文中,我们将讨论如何使用 PowerShell 将多行文本列添加到 SharePoint Online 列表。

如何将多行文本字段添加到 SharePoint Online 列表中?

要将多行文本字段添加到 SharePoint Online 中的列表,请按照下列步骤操作:

  1. 浏览 SharePoint Online 网站并导航到要添加多行文本列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,将类型指定为“多行文本”

    [玩转系统] SharePoint Online:使用 PowerShell 将多行文本列添加到列表

  4. 填写其他可选值,例如行数、文本类型、追加对现有文本的更改等,然后单击“确定”在 SharePoint Online 列表中创建多行文本列。

PowerShell 在 SharePoint Online 列表中创建多行文本列

o 添加多行文本列,您可以使用以下 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"

#Custom function to add column to list
Function Add-MultilineTextColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $IsRichText="FALSE",
        [Parameter(Mandatory=$false)] [string] $NumLines = "6",
        [Parameter(Mandatory=$false)] [string] $EnhancedRichText = "FALSE"
    )

    #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
            if($EnhancedRichText -eq "TRUE") #Enhanced Rich Text Mode
            {
                $FieldSchema = "<Field Type='Note' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' NumLines='$NumLines' RichText='TRUE' RichTextMode='FullHtml' IsolateStyles='TRUE' />"
            }
            else  #Plain Text or Rich Text
            {
                $FieldSchema = "<Field Type='Note' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' NumLines='$NumLines' RichText='$IsRichText' />"
            }
            
            $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="ProjectDescription"
$DisplayName="Project Description"
$Description="Enter the Project Description"
$IsRichText="FALSE" #FALSE for Plain text / TRUE for Rich text
$EnhancedRichText="FALSE" #FALSE for Rich text / TRUE for Enhanced Rich Text, Takes Precedence over IsRichText parameter value

#Call the function to add column to list
Add-MultilineTextColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -IsRichText $IsRichText -EnhancedRichText $EnhancedRichText 

PnP PowerShell 将多行文本字段添加到 SharePoint Online 列表

同样,对于 PnP PowerShell 脚本 - 使用 Add-PnPField cmdlet。


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

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

#Add Multiple lines of Text Field to list
Add-PnPField -Type Note -List $ListName -DisplayName "Project Description" -InternalName "ProjectDescription" -AddToDefaultView

此脚本将一个新的多行文本列添加到“项目”列表中,其显示名称为“项目描述”,内部名称为“项目描述”。您还可以从 XML 架构将多行文本字段添加到 SharePoint Online 列表。


#Define XML Schema for Multiline Text Field
$FieldXML= "<Field Type='Note' Name='ProjectDescription' ID='$([GUID]::NewGuid())' DisplayName='Project Description' Required ='FALSE' RichText='TRUE' RichTextMode='FullHtml' ></Field>"

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

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

取消回复欢迎 发表评论:

关灯