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

SharePoint Online: ลบคอลัมน์ของไซต์ออกจากชนิดเนื้อหาโดยใช้ PowerShell


ความต้องการ: ลบคอลัมน์ของไซต์ออกจากประเภทเนื้อหาใน SharePoint Online

จะลบคอลัมน์ออกจากประเภทเนื้อหาใน SharePoint Online ได้อย่างไร

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

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

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

คุณจำเป็นต้องเอาคอลัมน์ออกจากชนิดเนื้อหาใน SharePoint Online หรือไม่ 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 Remove-ColumnFromContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $SiteColumnName
    )

    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 content type from web
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the site        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in '$SiteURL'" -f Yellow
            Return 
        }

        #Get the column to delete from content type
        $ContentTypeFieldColl =  $ContentType.Fields
        $Ctx.Load($ContentTypeFieldColl)
        $Ctx.ExecuteQuery()

        $SiteColumn = $ContentTypeFieldColl | Where {$_.Title -eq $SiteColumnName }
        if($SiteColumn -eq $null)
        {
            Write-host "Site Column '$SiteColumnName' doesn't exists in content Type '$ContentTypeName'" -f Yellow
        }
        else
        {
            #Get the field link from content type
            $FieldLinkColl = $ContentType.FieldLinks
            $Ctx.Load($FieldLinkColl)
            
            #Remove field from content type
            $FieldLink = $FieldLinkColl.GetById($SiteColumn.Id)
            $FieldLink.DeleteObject()
            $ContentType.Update($true)
            $Ctx.ExecuteQuery() 

            Write-host "Site Column '$SiteColumnName' deleted from '$ContentTypeName' successfully!" -ForegroundColor Green
        }
  }
    Catch {
        write-host -f Red "Error Deleting Site Column from Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Project Template"
$SiteColumnName="Project Department" #Display Name of the Field

#Call the function
Remove-ColumnFromContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName

PnP PowerShell เพื่อลบคอลัมน์ออกจากประเภทเนื้อหาใน SharePoint Online

ถ้าคุณต้องการเอาคอลัมน์ออกจากชนิดเนื้อหาใน SharePoint Online คุณสามารถใช้ PnP PowerShell ได้! ฉันจะแสดงตัวอย่างการใช้ PowerShell เพื่อลบคอลัมน์ออกจากประเภทเนื้อหา


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$FieldName ="Project_x0020_Manager"

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

If($ContentType)
{
    #Get the Field from Content type
    $Field = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
    $Context.Load($Field) 
    $Context.ExecuteQuery() 

    #Remove the Field from content type
    $ContentType.FieldLinks.GetById($Field.Id).DeleteObject() 
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Field '$FieldName' has been removed from Content Type!"
}

หรือคุณสามารถใช้ cmdlet Remove-PnPFieldFromContentType ใน PnP PowerShell นี่คือวิธีการ:


Remove-PnPFieldFromContentType -Field $FieldName -ContentType $ContentTypeName

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