1

In SharePoint online, how do we remove Content Type from the Library by using Powershell ? I have tried using the CSOM code shown below but it doesn't seem to work:

$ctx.credentials = $creds  
$contentTypes = $ctx.web.contenttypes  
$ctx.load($contentTypes)  
$ctx.executeQuery() 

foreach($ct in $contentTypes){  
    write-host $ct.name  
} 


$ctName = "TCLDocument"
#Load the content types collection. 
$ctx.Load($ctx.Web.Lists)
$ctx.ExecuteQuery()
foreach( $ll in $ctx.Web.Lists)
{

    if($ll.Title -eq $ctName){
    $ctx.Load($ll.ContentTypes)

    try
    {
        $ctx.ExecuteQuery()
    }
    catch
    {
    }

    Write-Host $ll.Title -ForegroundColor Green

    foreach($cc in $ll.ContentTypes)
    {
        Write-Output $cc.Name
        if($cc.Name = "Document"){
            $ctExists = $true  
            $existingCT = $ct  
            Write-Host "Content Type $ctId 1 found" -ForegroundColor Red  
            break  
         }
    } 
    if($ctExists){  
        $existingCT.DeleteObject()  
        $ctx.ExecuteQuery() 

        Write-Host "Content Type $ctId 2 found" -ForegroundColor Red  
        break 
    }  
}

1 Answer 1

1

One of the things wrong with your script is:

if($ll.Title -eq $ctName)

That means you are checking if the list title is the same as the name of a content type. I don't think that's what you want to check.

You can also try using this script from Technet Gallery (aren't you using it already, just forgot to mention the source?) or Remove-SPOContentType cmdlet from this Powershell module - also from Technet Gallery. Sample use of the cmdlet:

Remove-SPOContentType -ContentTypeID "0x0100E6BD49EA28F1AE4BA49AA555FB4B179A" -ListTitle MyList6

Source: http://social.technet.microsoft.com/wiki/contents/articles/32531.sharepoint-online-spomod-remove-spocontenttype.aspx
If nothing of these will work, post an error message that you are getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.