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

Azure OpenAI Chat in C# and Python

Azure OpenAI Chat in C#: // Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5   using System; u...

Popular posts