Best way to find where content type is in use

I just happened across a handy class within the SharePoint object model which allows finding where a content type is currently in use within a site much quicker than using the custom code I had previously been using.  This would be handy when unable to delete a content type because it is in use, or if we simply want to know if a content type is in use somewhere within our SharePoint site.

SPContentTypeUsage
Used to track available content types used throughout a site collection.

Info:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttypeusage.aspx

Example: 

private void GetContentTypeUsage(string ctypeId)
{
 
// get content type from supplied id
 
SPContentType ctype = SPContext.Current.Web.ContentTypes[new SPContentTypeId(ctypeId)]; 

  // get list containing all references
 
IList<SPContentTypeUsage> usageList = SPContentTypeUsage.GetUsages(ctype); 

  if (usageList.Count > 0)
  {
   
for (int count = 0; count < usageList.Count; count++)
    {
     
// do something

      // usageList[count].IsUrlToList;
     
// usageList[count].Id;
     
// usageList[count].Url;
    }
  }
}

I found the above information while looking at the link below detailing the Microsoft.SharePoint Namespace:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.aspx

Advertisements
This entry was posted in SharePoint and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s