Saturday, 12 July 2014

Cannot delete content type :( start by finding its usages.

      
 using (SPSite siteCollection = new SPSite("http://localhost"))  
      {  
       using (SPWeb webSite = siteCollection.OpenWeb())  
       {  
         // Get the obsolete content type.  
         SPContentType obsolete = webSite.ContentTypes["Test"];  
         if (obsolete != null) // We have a content type  
         {  
          IList usages = SPContentTypeUsage.GetUsages(obsolete);  
          if (usages.Count > 0) // It is in use  
          {  
            Console.WriteLine("The content type is in use in the following locations:");  
            foreach (SPContentTypeUsage usage in usages)  
             Console.WriteLine(usage.Url);  
          }  
          else // It is not in use.  
          {  
            // Delete it.  
            Console.WriteLine("Deleting content type {0}...", obsolete.Name);  
            webSite.ContentTypes.Delete(obsolete.Id);  
          }  
         }  
         else // No content type is found.  
         {  
          Console.WriteLine("The content type does not exist in this site collection.");  
         }  
       }  
      }  
      Console.Write("\nPress ENTER to continue...");  
      Console.ReadLine();  
 

No comments:

Post a Comment