1

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

4 Answers 4

0

There are multiple elements

  • 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);
    
  • 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)

1

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.

0

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

0

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"

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.