1


I want to delete content type programmatically on feature deactivation. I've wrote the code to perform deletion:

         SPSecurity.RunWithElevatedPrivileges(delegate()
         {
            using (SPWeb webSite =(SPWeb)properties.Feature.Parent)
            {
                webSite.AllowUnsafeUpdates = true;
                // Get the obsolete content type.
                SPContentType obsolete = webSite.ContentTypes["Examples8888 - CT1"];

                // We have a content type.
                if (obsolete != null)
                {
                    IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);

                    // It is in use.
                    if (usages.Count <= 0)
                    {
                        obsolete.Delete();
                        // Delete it.
                        Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                        webSite.ContentTypes.Delete(obsolete.Id);
                    }
                }


            }
         });

But it gives me error :

The content type is part of an application feature.

This content type is not being used anywhere still I am not able to delete it.

Is there any way to deal with this error?

2

Content type deployed from feature will be removed from server automatically during feature deactivation, but only if it is not used anywhere (including lists and items in SP recycle bin). So, you don't need to remove it manually and actually, you can't remove it programmatically or in any other way except deleting from database (which is definitely not good).

If it's an orphaned content type - you can remove it from DB (read about it here).

  • Thanks Vilkov, so can I conclude that Content Type can not be removed programmatically? – Priya Nov 15 '13 at 5:29
  • It can be removed only if it was programmatically created, not via XML. If the content type is not in use, it will be removed by sharepoint, you don't need to force it. – Evgenii Vilkov Nov 15 '13 at 6:31
1

Some times contenttypes are not removed and the solution that provisioned the contenttype is retracted, or the contenttype is removed from the feature that deployed the contenttype.

Now the contenttype cannot be removed anymore because the contentype was part of an application feature.

Now you can remove it with sql from the DB, but that is not supported by Microsoft. It is not allowed to update or query or whatever on SharePoint databases.

Another way to remove this contenttype is create a SandBoxed solution with a feature. In this solution create a contenttype. The contentype need to have the same Id as the contenttype that needs to be removed. Deploy the sandboxed solution en then retract the solution.

The contentype should be removed

  • Thanks Remko, We have farm solution and we are creating content type using XML. I want to delete content type on feature deactivation if it is not in use.My client want it to work in that way. Can I conclude that Content Type can not be removed programmatically on feature deactivation? – Priya Nov 15 '13 at 5:41
  • If you use XML for to provision content types it will be removed on feature deactivation and retraction of the solution. When a contenttype is created by code it will not be removed by deactivating. Then this has to be done by code – Remko Nov 15 '13 at 8:47
0

To be more precise.. Again activate the content type feature then delete all list and library which refer to that content type and even from all the recycle bin (Site and Site collection both). Then deactivate feature it will remove content type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.