Archive for March, 2010

Deleting Content Types that are still in use

If you are like me, you tend to deal with strange development nuances in the SharePoint environment.  One of those nuances is trying to delete a content type that is in use.   When I’m developing a solution that requires the creation of content types within the feature, I typically create that content type through code.  That will work on the first deployment of the feature since it does not exist yet.  On subsequent deployments, you will get an error that either the content type is in use (if you are trying to delete it before adding) or the content type already exists.  One thing I do is disable the deletion/creation of the content type if I’m not making any changes to it on subsequent deployments.  That will save a bit of development time.  The second problem I get every so often is the “content type still in use” error when trying to delete a content type.  I find this to happen if I am careless with removing items from a list, especially workflow tasks that are associated with a content type. 

I found a decent SQL Script (below) on the interweb that displays all the instances of my content type in the SharePoint databases.  In some cases, this will tell me where the “remnant” of the content type is and I can remove it.  Other times, it will display something that I already removed and I’m stuck.  If that happens, I first blow away the list where the content type was associated.  If the list removal doesn’t work, I will delete the site collection and build a new one.  (That is a luxury of having my own development VM.) I found that course of action to be quicker than trying to go through the database structure to find where to delete all instances of that content type.

 

Code Snippet
  1. SET @ContentTypeName='My Conent Type'
  2. SELECT w.Title AS [Web Site], w.FullUrl AS [Web Url], al.tp_Title AS [List Title], ct2.* FROM ContentTypes ct1 JOIN ContentTypes ct2 ON LEFT(ct2.ContentTypeId, Len(ct1.ContentTypeId))=ct1.ContentTypeId LEFT OUTER JOIN dbo.ContentTypeUsage ctu ON LEFT(ctu.ContentTypeId, Len(ct2.ContentTypeId)) = ct2.ContentTypeId LEFT OUTER JOIN dbo.AllLists al ON ctu.ListId = al.tp_Id AND ctu.WebId=al.tp_WebId LEFT OUTER JOIN dbo.Webs w ON al.tp_WebId = w.Id WHERE ct1.ResourceDir=@ContentTypeName

Sources: 1

Leave a comment