This topic has not yet been rated - Rate this topic

SPBuiltInContentTypeId Class

A class that retrieves SPContentTypeId objects that represent identifiers (IDs) for built-in content types.

System.Object
  Microsoft.SharePoint.SPBuiltInContentTypeId

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
public sealed class SPBuiltInContentTypeId

You can use the fields of this class in a way that is similar to how you use members of an enumeration.

The following example is a console application that examines where the built-in “Item” content type is used in a site collection. The application begins by building a generic list of SPContentTypeUsage objects that contain information about each use of a content type in a site collection. Then it counts the number of times that the content type is used as a site content type and the number of times it is used as a list content type. The results are printed to the console.

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 rootWeb = siteCollection.RootWeb)
            {
               // Get the content type.
               SPContentType contentType =
                  rootWeb.AvailableContentTypes[SPBuiltInContentTypeId.Item];

               //Get the usage collection.
               IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);

               // Count the site and list types.
               int listTypes = 0;
               int siteTypes = 0;
               foreach (SPContentTypeUsage usage in usages)
               {
                  if (usage.IsUrlToList)
                     listTypes++;
                  else
                     siteTypes++;
               }

               Console.Write("The content type is inherited by {0} site content types", siteTypes);
               Console.WriteLine(" and {0} list content types.", listTypes);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

When the application is run against a Web site created with the Team Site template, it prints the following output to the console.

The content type is inherited by 33 site content types and 20 list content types.

Press ENTER to continue...
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
SPBuiltInContentTypeId
Description

The Microsoft.SharePoint.SPBuiltInContentTypeId sealed class is useful for two distinct reasons. Firstly, it contains a series of static Microsoft.SharePoint.SPContentTypeId fields that characterize the default SharePoint content types. This is helpful for historical comparisons. Secondly, it provides the Contains method which takes a SPContentTypeId object as a parameter. Contains creates a typed Dictionary of the previously described SPContextTypeId fields, and then attempts to get the passed SPContentTypeId from the dictionary. This allows you to query whether a specific SPContentTypeId is one of the default, built-in ones supplied by SharePoint. 

Usage Scenario

The usage of SPBuiltInContentTypeId is as described above, either to use for field reference or to check whether a particular SPContentTypeId is one of the built-in SharePoint content types. The former of these is commonly seen as the fundamental use. 

In the below, the first example is hydrating a SPContentTypeId object by passing in literal content type GUID. Once the object is hydrated, we can use SPContentTypeId.IsChildOf passing in SPBuiltInContentTypeId.Announcement which will compare the derivation for Announcements. 

Secondly, we are demonstrating the basic use of the SPBuiltInContentTypeId.Contains method.

C# Code Example

bool isChild;
bool isDefault;
SPContentTypeId contentTypeId = new SPContentTypeId ("guid of the target content type");
if (contentTypeId.IsChildOf(SPBuiltInContentTypeId.Announcement))
{
isChild = true;
}
if (SPBuiltInContentTypeId.Contains(contentTypeId))
{
isDefault = true;
}

Visual Basic .NET Code Example

Dim isChild As Boolean
Dim isDefault As Boolean
Dim contentTypeId As New SPContentTypeId("guid of the target content type")
If contentTypeId.IsChildOf(SPBuiltInContentTypeId.Announcement) Then
isChild = True
End If
If SPBuiltInContentTypeId.Contains(contentTypeId) Then
isDefault = True
End If

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com