6

I have a custom content type and I added this content type to a list. I now want this list to only use the custom content type and I want to remove the built-in "Item" content type.

I tried

$spHeaderList.ContentTypesEnabled = $true
$spHeaderList.ContentTypes.Delete($spItemContentTypeId)
$spHeaderList.Update()

I used $spItemContentTypeId as 0x01 and as

$ItemID = [Microsoft.SharePoint.SPBuiltInContentTypeId]::Item
$spItemContentType = $spWeb.ContentTypes[$ItemID]

but still could not get it to work. I always get the error message that

"Cannot convert null to type "Microsoft.SharePoint.SPContentTypeId"

Any clues?

I am using SP2010

3 Answers 3

2

Don't make it harder on yourself than needed - instead of trying to figure out the IDs yourself, just look for the plain text name you know. There is a good tutorial on how to remove a content type via powershell from a list here: Add and remove site content types from SharePoint lists using PowerShell.

Please note that you can only remove the "Item" content type from your list if there are no objects with the item content type currently in your list. The Powershell script blatantly copied below:

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://YOURSITEHERE
$lookForList = "YOURLISTHERE”

#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {

    write-host "Checking site:"$_.Title

    #Check list exists
    $docLibrary = $_.Lists[$lookForList]

    #Remove unwanted content types from the list
    if($docLibrary -ne $null)
    {
        $ctToRemove = $docLibrary.ContentTypes["Item"]
        write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
        $docLibrary.ContentTypes.Delete($ctToRemove.Id)
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()
0

The PowerShell code which can be helpful to remove ITEM content type from custom list.

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    $web = Get-SPWeb "UrlOfYourSite"
    $list = $web.Lists["YourListName"]
    $ctToRemove = $list.ContentTypes["Item"]
    if($ctToRemove)
    {
        Write-Host "Removing Item content type"
        $list.ContentTypes.Delete($ctToRemove.Id)
        $list.Update()
        $Web.Dispose()
    }
    else
    {
        Write-Host "Item content type do not exists in list"
    }
0

The issue with your code is, that in a list content types are not added directly but only as a child of the web content type. This means, that no content type on a list will ever have the ID 0x01.

It is always better to use the SPBuiltInContentTypeId to refer to an out of the box content type, because the name change with the language SharePoint is installed in - which will break any other code.

To delete the item content type that way, simply find the Content Type with Item as parent:

$ItemCTID = [Microsoft.SharePoint.SPBuiltInContentTypeId]::Item;

foreach($ct in $spList.ContentTypes)
{
    if($ct.Parent.ID -eq $ItemCTID)
    {
        $spList.ContentTypes.Delete($ct.ID);
        break;
    }
}

Note that you should only use this for one content type or it will break the foreach. This removes ONLY content types in the list with Item as direct parent. Meaning if your custom web content type inherits from Item as well, it will not be removed.

Your Answer

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

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