This topic has not yet been rated - Rate this topic

SPContentTypeUsage Class

Used to track where a content type is used as the basis for another content type.

System.Object
  Microsoft.SharePoint.SPContentTypeUsage

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SubsetCallableTypeAttribute]
public sealed class SPContentTypeUsage

An SPContentTypeUsage object contains information about a single instance where a content type is used as the basis for another content type. For example, when a site content type is applied to a list, a child of the site content type is created and added to the list’s content type collection. The new list content type is an instance where the parent site content type is “used.” An SPContentTypeUsage object can provide information about this use, such as the Uniform Resource Locator (URL) for the root folder of a list that has a derived content type.

Note Note

A content type is “used” if any content type derived from it is present in an SPContentTypeCollection collection at the site or list level anywhere within the scope of the parent content type.

To get a list of all uses of a content type throughout a site collection, call the static GetUsages(SPContentType) method, which returns a generic list of SPContentTypeUsage objects.

The following example shows a console application that verifies whether an obsolete content type is in use in the current site or any child sites. If the content type is not in use, the application deletes it.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         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<SPContentTypeUsage> 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();
      }
   }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
GetUsages Method Not Available in Sandboxed Solution
The GetUsages static method isn't availabe within the sandboxed environments. The documentation indicates the class is avaiable, but this particular method isn't supported. The method generate an unhandled exception which indicates the same within a debugging session. Examining the ULS log shows the inner exception thrown within Microsoft.SharePoint.UserCoce.SPUserCodeSolutionProciedException. It also states that the System.Collection.Generic IList <Microsoft.SharePoint.SPContentTypeUsage> Microsoft.SharePoint.SPContentTypeType.GetUsages(Microsoft.SharePoint.SPContentType) isn't available.
Switching my project to the User Code restrictedc Microsoft.SharePoint subset assembly verfies my findings. The compilation process fails. The unavailability of the method is the cause shown. The sample code shown in the class documentaton is as follows:

IList<SPContentTypeUsages> usages = SPContentTypeUsages.GetUsages(myContentType);

The code statement is the exact cause of the runtime failure within a Sandboxed Soluiton.

The SharePoint Online reference should be updated to reflect this condition.