SharePoint Online: Remove a Content Type from All Lists in a Site Collection using PowerShell
Requirement: Remove Content Type from All Lists and Libraries in a SharePoint Online Site Collection
PowerShell to Remove Content Type from All Lists and Libraries in a Site Collection
This PowerShell script iterates through all lists and libraries of a given site collections, scans for provided content type and removes it, if its not being used!
Related posts:
PowerShell to Remove Content Type from All Lists and Libraries in a Site Collection
This PowerShell script iterates through all lists and libraries of a given site collections, scans for provided content type and removes it, if its not being used!
#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 to remove content type from a site collection Function Remove-SPOContentType([String]$SiteURL, [String]$ContentTypeName) { Try{ Write-host -f Yellow "Processing Site:" $SiteURL #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Get All Lists of the Web $Ctx.Load($Ctx.Web) $Ctx.Load($Ctx.Web.Lists) $Ctx.Load($ctx.Web.Webs) $Ctx.ExecuteQuery() #Get content types of each list from the web ForEach($List in $Ctx.Web.Lists) { $ContentTypes = $List.ContentTypes $Ctx.Load($ContentTypes) $Ctx.ExecuteQuery() #Get each content type data ForEach($CType in $ContentTypes) { If($CType.Name -eq $ContentTypeName) { Try{ #Remove Content Type from List $CType.DeleteObject() $Ctx.ExecuteQuery() Write-host -f Green "Removed Content Type from List/Library:" $List.Title } Catch { write-host -f Red "`tError Removing Content Type from '$($List.Title)' :" $_.Exception.Message } } } } #Iterate through each subsite of the current web and call the function recursively foreach ($Subweb in $Ctx.web.Webs) { #Call the function recursively to process all subsites underneaththe current web Remove-SPOContentType $Subweb.url $ContentTypeName } } Catch { write-host -f Red "Error Removing Content Type:" $_.Exception.Message } } #Config Parameters $SiteURL="https://crescent.sharepoint.com/sites/marketing" $ContentTypeName="Project Proposal" #Get Credentials to connect $Cred= Get-Credential #Call the function to get the content type usage Remove-SPOContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName
Related posts:
No comments:
Please Login and comment to get your questions answered!