kwizcom banner advertisement

SharePoint Online: Delete Content Type using PowerShell

Requirement: SharePoint Online Delete Content Type from site.

How to delete content type in SharePoint Online?
To delete a content type in SharePoint online, follow these steps:
  • Login to your SharePoint online site >> Click on Site settings gear >> Site Settings 
  • In the Site settings page, Click on "Site Content Types" under 
  • Under Site Content Types page, Click on the name of your content type you wish to delete. 
  • Click on "Delete this site content type " link and confirm the prompt once.
    sharepoint online delete content type using powershell

Delete Content Type using PowerShell in SharePoint Online:
Here is the PowerShell to delete content type in SharePoint Online.
#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 Delete-ContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )

    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
        }
        else
        {
            #delete the content type
            $ContentType.DeleteObject()
            $Ctx.ExecuteQuery() 

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

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Projects"

#Call the function
Delete-ContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName

Cannot Delete Content Type in SharePoint Online? 
Can't delete the content type? Getting "Another site or list is still using this content type. If you would still like to delete it, please remove the content type from all sites and lists and then try again." Error? Well,  This could be because, you may have lists or libraries associated with the content type you are trying to delete. Here is the solution: SharePoint Online cannot delete content type
SharePoint Online: Delete Content Type using PowerShell SharePoint Online: Delete Content Type using PowerShell Reviewed by Salaudeen Rajack on April 20, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.