Friday, August 19, 2011

Using the Object Model to Delete a Content Type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Test{
    class Program
    {
      static void Main(string[] args)
      {         
         using(SPSite siteCollection = new SPSite("http://sharepoint:24433"))
         {
            using(SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get the obsolete content type.
               SPContentType obsolete = webSite.ContentTypes["Colour Calendar Mapping"];

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

                  // It is in use.
                  if (usages.Count > 0)
                  {
                     Console.WriteLine("The content type is in use in the following locations:");
                     foreach (SPContentTypeUsage usage in usages)
                        Console.WriteLine(usage.Url);
                  }

                  // The content type is not in use.
                  else
                  {
                      try
                      {
                          // Delete it.
                          Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                          webSite.ContentTypes.Delete(obsolete.Id);
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("NOT Possible to Delete, Because "+e.Message);
                      }
                  }
               }

               // No content type found.
               else
               {
                  Console.WriteLine("The content type does not exist in this site collection.");
               }
            }
         }
          Console.Write("\nPress ENTER to continue...");
          Console.ReadLine();
          }
        }
    }
Reference: Using the Object Model to Delete a Content Type

No comments:

Post a Comment

Featured Post

SharePoint Edit Control Block (ECB) menu or Custom Action Menu In ListItem and Site

 SharePoint Edit Control Block (ECB) menu or Custom Action Menu In ListItem and Site: < script   language = "javascript"   type...

Popular posts