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

2 comments:

  1. BD3AA67449
    In today's digital age, having a strong online presence is essential for any business. Many companies find that optimizing their website can significantly improve customer engagement and sales. By focusing on user experience and content quality, businesses can stand out in a competitive market. For more insights, visit website.

    ReplyDelete

Featured Post

Managing All Group Types with CRUD Operations

Managing All Group Types with CRUD Operations Introduction Managing groups in Microsoft 365 is a routine task for administrators and d...

Popular posts