Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created document Library programmatically and add some custom Content Types to it. But I want to Delete/remove default "Document" content type from my document library. How i do that ?

library.ContentTypes.Delete(CT.Id); // it never works

Any Other idea to do this ? Or piece of code

share|improve this question

4 Answers 4

up vote 0 down vote accepted

There are multiple elements

  1. You cannot delete the last content type in a list/library. This practically means that you need to add another Content Type (inheriting from Document) before removing the out-of-the-box content types. Depending on the structure of your solution you can either create a new content type programatically, as in the following code. Or you could create this declaratively in Elements.xml:

    private static string ctName = "Contract"; private static SPContentTypeId parentId = SPBuiltInContentTypeId.Document; SPContentType ct = new SPContentType(web.ContentTypes[parentId], web.ContentTypes, ctName);

  2. You would also need to configure "Enable management" at the list level of content types prior to executing this function (programatically also possible via SPList.EnableContentType=true)

share|improve this answer

How are you declaring the variable CT? (you are using CT.Id for the delete command) Does it point to the List "Document" content type, or the web "Document" content type?

This link talks about removing a default content type -> possibly the code sample in the first answer might provide the answer you're looking for:

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/944cad8b-6e15-43e1-bca6-6f6cb29383e6

share|improve this answer

I've got a Powershell function I use to update content types in a list/library and delete a content type too.

    function Update-CT {
        [CmdletBinding()]    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string]$Web,
        [Parameter(Mandatory=$true)]
        [string]$ListTitle,
        [Parameter(Mandatory=$true)] 
        [string]$ContentTypeAdd,
        [Parameter(Mandatory=$true)] 
        [string]$ContentTypeRemove  
        )

        Start-SPAssignment -Global
        $SPWeb = Get-SPWeb -Identity $Web
        $docLibrary = $SPWeb.Lists[$ListTitle]
        if ($docLibrary -ne $null)
        {
            $docLibrary.ContentTypesEnabled = $true
            $docLibrary.Update()
            #Add site content types to the list
            $ctToAdd = $SPWeb.ContentTypes[$ContentTypeAdd]
            $ct = $docLibrary.ContentTypes.Add($ctToAdd)
            write-host "Content type" $ct.Name "added to list" $docLibrary.Title
            $ctToRemove = $docLibrary.ContentTypes[$ContentTypeRemove]

            write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
            $docLibrary.ContentTypes.Delete($ctToRemove.Id)
            $docLibrary.Update()
        }

        Stop-SPAssignment -Global
    }

Update-CT -Web "http://sharepoint" -ListTitle ListName -ContentTypeAdd "My Content Type" -ContentTypeRemove "Document"
share|improve this answer

A nice, and pretty safe way to remove a known content type from a list is to use code like this:

// Remove default Document content type
SPContentTypeId listDocumentCT = list.ContentTypes.BestMatch(SPBuiltInContentTypeId.Document);
if (listDocumentCT.IsChildOf(SPBuiltInContentTypeId.Document))
    list.ContentTypes.Delete(listDocumentCT);

This code removes the default Document content type from a list.

The challenge is to get the content type id of the content type that's used in the List, which isn't the same as the content type id of the Site content type's id. This is because adding a content type to a list creates a new content type with the same name but with a different ID in the list. Using SPContentTypeCollection.BestMatch(...) helps finding this content type id.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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