Dealing with the “The content type is part of an application feature” error

Error when trying to remove an orphaned Content Type
When working with custom Lists and Content Types you might get yourself into a situation where you end up with an orphaned Content Type that cannot be removed. There might be however a solution to this issue.

Important: Although the example used for this article is based on SharePoint 2013 the same issue and solution apply to SharePoint 2010.

The error aka. “My Content Type” is a part of an application feature

Imagine the following scenario: you have a Solution Package with a custom Content Type, custom List Definition using your Content Type and a custom List created from your custom List Definition.

Sample project structure in Visual Studio 2013

When installing your solution, you would deploy the Solution Package and activate the Site Collection Feature to install your Content Type and List Definition and create the List Instance.

Site Collection Feature activated in the overview of all Site Collection Features

Custom List Instance created from the custom List Definition with the custom Content Type associated

Whenever you would want to uninstall your Solution, you would typically delete the List Instance and deactivate the Site Collection Feature. Unfortunately this will leave you with the orphaned My Content Type Content Type.

The ‘My Content Type’ Content Type listed in the overview of Site Content Type after deactivating the Site Collection Feature

What’s even worse is, that if you try to delete the Content Type, you will get an error similar to the following:

The content type "My Content Type" is part of an application feature.

Exception when trying to manually remove the orphaned Content Type

So if our custom Content Type has been deployed with a Feature why hasn’t it been uninstalled when we deactivated it?

The reason

When deploying Content Types using Features in SharePoint, they are being flagged with the IsFromFeature flag in the Content Database. This prevents them from being accidentally deleted as well as it tells SharePoint to remove those Content Types when deactivating the particular Feature. So why sometimes Content Types are not being deleted when deactivating their Feature despite the fact that they have been deployed by a Feature of which SharePoint keeps track?

One reason for that is that the particular Content Type might still be in use. Whether a Content Type is in use or not can be easily verified by using the SPContentTypeUsage API:

using (SPSite site = new SPSite("http://mavention")) {  
    using (SPWeb web = site.OpenWeb()) {
        SPContentType ct = web.ContentTypes[new SPContentTypeId("0x01002F7123FC131346B1A674B83AB89841F1")];
        IEnumerable<SPContentTypeUsage> u = SPContentTypeUsage.GetUsages(ct);
    }
}

Content Type usage confirmed using the SharePoint API

After removing the List, the Content Type Usage list is empty – just as you would expect.

Content Type Usage list empty after removing the List

So if the Content Type isn’t used anymore, why is it not being deleted?

The real reason

Even though the List has been removed and the Content Type Usage list is empty, the Content Type is still in use. This has to do with the fact that deleting a List in SharePoint doesn’t permanently remove it, but merely moves it to the Recycle Bin. The confusing part is, that while the Content Type is still in use by the List in the Recycle Bin, the Content Type Usage list is empty what makes this issue so hard to track.

The solution

In order to prevent yourself from ending up with an orphaned Content Type, before you deactivate the Site Collection Feature that installed the Content Type, you should:

  • delete the List from the Site
  • remove the List from the Site Recycle Bin (/_layouts/15/RecycleBin.aspx) Custom List in the Site Recycle Bin
  • remove the List from the Site Collection Recycle Bin (/_layouts/15/AdminRecycleBin.aspx?View=2, Deleted from end user Recycle Bin) Custom List in the Site Collection Recycle Bin

Following those steps the Content Type will not be in use anymore and will be removed from your site when deactivating the Site Collection Feature.

Summary

When working with custom Content Types and List Definitions you might get yourself into a situation where you end up with an orphaned Content Type that cannot be removed. In order to properly remove such Content Type you have to deletes all Lists using that Content Type not only from their respective sites but also from the Site and Site Collection Recycle Bin before deactivating the Site Collection Feature.

Comments

comments powered by Disqus