Wednesday, February 21, 2007

Delete a contenttype from a document library

You can find a lot of information about how to add a contenttype (SPContentType) to a list or document library (SPDocumentLibrary). But how do you delete a contenttype from a document library?

From the administration UI you can remove a contenttype by selecting "Delete this content type" as outlined here:

But how do you do it programmatically?`

If you take a look at SPDocumentLibrary it has a reference to ContentTypes. The collection has a delete operation which takes a GUID of the content type as parameter, as outlined by Microsoft here.

dim oDocLib as SPDocumentLibrary
dim oCT as SPContentType
oCT = oDocLib.ContentTypes("Documents")
oDocLib.ContentTypes.Delete(oCT.Id)

Trying out this code raises an exception:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: id
at Microsoft.SharePoint.SPContentTypeCollection.Delete(SPContentTypeId id)

Inputs very much appreciated.

I got the idea to try using the Lists.asmx web service as described here by Microsoft. No luck so far.

You can get around the issue as described here. Set the content type programmatically before you add the items - but that is not really the answer long term :-(

Conclusion: Now it works!!!
I tried to delete the Documents content type found in the web collection AvailableContentType i.e. executing the code:

dim oWeb as SPWeb
dim oDocLib as SPDocumentLibrary
dim oCT as SPContentType
oCT = oWeb.AvailableContentTypes("Documents")
oDocLib.ContentTypes.Delete(oCT.Id)

this fails as described above.

The above code works.

11 comments:

Anonymous said...

Is there a possibility to delete a Content Type in Sharepoint (not procrammatically) when SP says, 'The Content type is in use'?

because it isnt in use :) There have to be a mistake with the content type and I have to 'hard' delete it.

Is there any way?

Thanks..

Morten Marquard said...

Guni,
If you get the error when trying to delete a content type "content type in use" it is most likely because it is in usage. You must delete any reference to the content type before you can delete it - so look in any document library you might have created and added the content type to. Also - you might ned to delete it from the site as a site content type- before you can delete it at the main site.

Anonymous said...

I need to do this now; to remove the "Item" Content Type from a List that I have created through the Object Model. Whether I use AvailableContentTypes or not I still get the exception.

Are you sure this works for you?

Morten Marquard said...

Hi,
This is the exact code that works fine:
Dim oCT As SPContentType
oCT = oDocLib.ContentTypes.Item("<content type>")
oDocLib.ContentTypes.Delete(oCT.Id)
oDocLib.Update()

Morten

Anonymous said...

Is there a way in the Site Settings for a Document Library to remove the content types?

At the moment it seems I have to delete the the whole document library and start from scratch.

Anonymous said...

Hi Morten

With reference to your comment to Guni below.

In my situation the content type is marked as in use as it is associated with a document library - it is defintely not used by any document anywhere in my deployment.

I presume it can't be deleted due to its relationship with the library but there is no UI provision for removing available content types from a library.

Any ideas? I'd hate to delete the whole library.

Roy

Guni,
If you get the error when trying to delete a content type "content type in use" it is most likely because it is in usage. You must delete any reference to the content type before you can delete it - so look in any document library you might have created and added the content type to. Also - you might ned to delete it from the site as a site content type- before you can delete it at the main site.

Kyle said...

I had a similar issue- here is how I got it to work. I started at the document libraries where I had turned on the content type I wanted to delete- the first step was to go into the explorer view for the library - and delete the content type folder from the forms folder. This clears up and docs in check-out limbo because of missing meta-data. I then went into the document library settings and into the content type and deleted it. If you have ever turned on the content type for a library and then turned disabled content types later- you will need to re-enable content types and then delete it from the libraries. After cleaning the content type out of all the libraries- you can go to the site settings and remove the content type without getting the error.

Anonymous said...

hay do you know how to create a folder or a file of a content type in a Document Library?

Gaurav Kanwar said...

Hi I tried this and it worked for me

SPContentType ctype = oDoc.ContentTypes["ContenTypename"];
ctype.Delete();

Anonymous said...

You can remove custom content types that you created in Visual Studio by 'Retracting'.
Right click the project and click Retract.

Anonymous said...

Thanks Kyle - it worked!! No code necessary!