Поделиться через


SPContentTypeUsage Class

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

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPContentTypeUsage

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public NotInheritable Class SPContentTypeUsage

Dim instance As SPContentTypeUsage
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public sealed class SPContentTypeUsage

Remarks

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 detailed information about this use, such as the Uniform Resource Locator (URL) of the root folder of the list.

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.

Examples

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.

Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp

   Sub Main()
      Using siteCollection As SPSite = New SPSite("http://localhost")
         Using webSite As SPWeb = siteCollection.OpenWeb()

            ' Get the obsolete content type.
            Dim obsolete As SPContentType = webSite.ContentTypes("Test")

            If obsolete IsNot Nothing Then ' We have a content type
               Dim usages As IList(Of SPContentTypeUsage) = _
                                   SPContentTypeUsage.GetUsages(obsolete)
               If usages.Count > 0 Then ' It is in use

                  Console.WriteLine("The content type is in use in the following locations:")
                  For Each usage As SPContentTypeUsage In usages
                     Console.WriteLine(usage.Url)
                  Next usage

               Else ' It is not in use.
                  ' Delete it.
                  Console.WriteLine("Deleting content type {0}...", obsolete.Name)
                  webSite.ContentTypes.Delete(obsolete.Id)
               End If

            Else ' No content type available.
               Console.WriteLine("The content type does not exist in this site collection.")
            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

End Module
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();
      }
   }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPContentTypeUsage Members

Microsoft.SharePoint Namespace

Other Resources

Site and List Content Types

Creating Content Types Based on Other Content Types

Content Type Scope