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

SharePoint Online: เพิ่มคอลัมน์ลงในรายการประเภทเนื้อหาโดยใช้ PowerShell


ความต้องการ: เพิ่มฟิลด์เพื่อแสดงรายการประเภทเนื้อหาใน SharePoint Online

วิธีเพิ่มคอลัมน์ลงในรายการหรือไลบรารีประเภทเนื้อหาใน SharePoint Online

PowerShell เป็นภาษาสคริปต์ที่มีประสิทธิภาพซึ่งสามารถช่วยทำให้งานต่างๆ ใน SharePoint เป็นแบบอัตโนมัติได้ อาจมีบางครั้งที่คุณจำเป็นต้องเพิ่มคอลัมน์ลงในชนิดเนื้อหารายการ คุณสามารถทำได้ด้วยตนเอง แต่อาจใช้เวลานานถ้าคุณมีรายการและชนิดเนื้อหาจำนวนมาก โพสต์ในบล็อกนี้จะแสดงวิธีการเพิ่มคอลัมน์ลงในชนิดเนื้อหารายการ SharePoint Online โดยใช้ PowerShell

ถ้าคุณต้องการเพิ่มเขตข้อมูลลงในรายการหรือชนิดเนื้อหาของไลบรารี ให้ทำตามขั้นตอนเหล่านี้:

  1. ไปที่การตั้งค่ารายการหรือไลบรารี >> คลิกที่ชื่อประเภทเนื้อหาที่เหมาะสมจากหน้าการตั้งค่ารายการ
  2. บนหน้าประเภทเนื้อหารายการ ใต้ส่วนคอลัมน์ ให้คลิกลิงก์ "เพิ่มจากไซต์หรือคอลัมน์รายการที่มีอยู่"
  3. เลือกกลุ่มคอลัมน์ที่เหมาะสม จากนั้นเลือกคอลัมน์ที่คุณต้องการเพิ่มลงในประเภทเนื้อหา คลิกที่ปุ่ม "เพิ่ม" จากนั้นคลิกตกลงเพื่อเพิ่มคอลัมน์นั้นลงในประเภทเนื้อหาของคุณ

คุณสามารถเพิ่มคอลัมน์จากคอลัมน์ของรายการที่มีอยู่หรือจากคอลัมน์ของไซต์ก็ได้

SharePoint Online: PowerShell เพื่อเพิ่มฟิลด์ลงในรายการประเภทเนื้อหา

มาเพิ่มฟิลด์เพื่อแสดงรายการประเภทเนื้อหาโดยใช้ PowerShell ต่อไปนี้เป็นวิธีใช้ 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"

Function Add-ColumnToListContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $ColumnName,
        [Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
    )

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

        #Get the content type from list
        $ContentTypeColl = $List.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the list        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
            Return 
        }

        #Get the column to add: Either site column or existing list's column
        If($IsSiteColumn)
        {
            $ColumnColl = $Ctx.Web.Fields
        }
        else #List Column
        {
            $ColumnColl = $List.Fields
        }
        $Ctx.Load($ColumnColl)
        $Ctx.ExecuteQuery()
        $Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
    
        #Check if given column exists
        if($Column -eq $Null)
        {
            Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
            Return
        }
        else
        {
            #Check if column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Ctx.Load($FieldCollection)
            $Ctx.ExecuteQuery()
            $Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
            if($Field -ne $Null)
            {
                Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
                Return
            }
    
            #Add field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $Column
            [Void]$ContentType.FieldLinks.Add($FieldLink)
            $ContentType.Update($false)
            $Ctx.ExecuteQuery() 
       
            Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false

#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName 

ตามค่าพารามิเตอร์ "IsSiteColumn" สคริปต์นี้สามารถเพิ่มคอลัมน์ของไซต์หรือคอลัมน์รายการลงในประเภทเนื้อหาที่กำหนดได้

PnP PowerShell เพื่อเพิ่มคอลัมน์เพื่อแสดงรายการประเภทเนื้อหาใน SharePoint Online


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = "Crescent Project V2"
$FieldName = "Department" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get the Content Type from the List
    $ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

    #Add a column list
    $Field =  Add-PnPField -List $ListName -DisplayName $FieldName -InternalName $FieldName -Type Choice -Choices "Sales","Marketing","Purchase" -AddToDefaultView

    #Add the column to Content type
    Add-PnPFieldToContentType -Field $Field -ContentType $ContentType
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

หากคุณต้องการเพิ่มเขตข้อมูลลงในประเภทเนื้อหาของไซต์ ให้ใช้: SharePoint Online: วิธีเพิ่มคอลัมน์ของไซต์ลงในประเภทเนื้อหาโดยใช้ PowerShell

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