kwizcom banner advertisement

SharePoint Online: Remove a Site Column from Content Type using PowerShell

Requirement: Remove a Site Column from Content Type in SharePoint Online.

PowerShell to Remove a Site Column from Content Type using 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
SharePoint Online: Remove a Site Column from Content Type using PowerShell SharePoint Online: Remove a Site Column from Content Type using PowerShell Reviewed by Salaudeen Rajack on 8:28 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.