ค้นหาเว็บไซต์

SharePoint Online: เพิ่มคอลัมน์ข้อความหลายบรรทัดลงในรายการโดยใช้ PowerShell


คอลัมน์ข้อความหลายบรรทัดใน SharePoint Online ใช้เพื่อจัดเก็บค่าข้อความหลายแถว ขึ้นอยู่กับการตั้งค่าฟิลด์ สามารถมี Rich Text รวมถึงข้อความ รูปภาพ ค่าที่จัดรูปแบบ HTML ฯลฯ ในบทความนี้ เราจะกล่าวถึงวิธีการเพิ่มคอลัมน์ข้อความหลายบรรทัดลงในรายการ SharePoint Online โดยใช้ PowerShell

จะเพิ่มช่องข้อความหลายบรรทัดลงในรายการใน SharePoint Online ได้อย่างไร

เมื่อต้องการเพิ่มเขตข้อมูลข้อความหลายบรรทัดลงในรายการใน SharePoint Online ให้ทำตามขั้นตอนเหล่านี้:

  1. เรียกดูไซต์ SharePoint Online ของคุณและนำทางไปยังรายการเป้าหมายที่คุณต้องการเพิ่มคอลัมน์ข้อความหลายบรรทัด
  2. ใต้แท็บรายการคลิกปุ่ม "สร้างคอลัมน์" ใน Ribbon
  3. ระบุชื่อให้กับคอลัมน์ใหม่ของคุณ ระบุประเภทเป็น “ข้อความหลายบรรทัด”

  4. กรอกค่าเสริมอื่นๆ เช่น จำนวนบรรทัด ประเภทข้อความ เพิ่มการเปลี่ยนแปลงต่อข้อความที่มีอยู่ ฯลฯ แล้วคลิก "ตกลง" เพื่อสร้างคอลัมน์ข้อความหลายบรรทัดในรายการ SharePoint Online

PowerShell สร้างคอลัมน์ข้อความหลายบรรทัดในรายการ SharePoint Online

หากต้องการเพิ่มคอลัมน์ข้อความหลายบรรทัด คุณสามารถใช้สคริปต์ PowerShell ต่อไปนี้:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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

สคริปต์นี้เพิ่มคอลัมน์ข้อความหลายบรรทัดใหม่พร้อมชื่อที่แสดง “คำอธิบายโครงการ” และชื่อภายใน “ProjectDescription” ในรายการ “โครงการ” คุณยังสามารถเพิ่มเขตข้อมูลข้อความหลายบรรทัดลงในรายการ SharePoint Online จาก XML Schema ได้


#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

บทความที่เกี่ยวข้อง