共用方式為


Content Type IDs

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

Content type IDs uniquely identify the content type and are designed to be recursive. The content type ID encapsulates that content type's lineage, or the line of parent content types from which the content type inherits. Each content type ID contains the ID of the parent content type, which in turn contains the ID of that content type's parent, and so on, ultimately back to and including the System content type ID. SharePoint Foundation uses this information to determine the relationship between content types and for push-down operations.

You can construct a valid content type ID using one of two conventions:

  • Parent content type ID + two hexadecimal values (the two hexadecimal values cannot be "00")

  • Parent content type ID + "00" + hexadecimal GUID

Note

A content type ID must be unique within a site collection.

In general, the first content type ID generation technique emphasizes brevity in that it only takes two hexadecimal digits to indicate a new content type. The second approach emphasizes uniqueness by including a GUID to indicate the new content type. Each approach is suited to certain situations.

Parent Content Type ID + Two Hexadecimal Values

SharePoint Foundation uses this convention to generate content type IDs for the default content types that are included with the platform. For example, the content type ID of the basic content type Item is 0x01. This indicates that the Item content type is a direct child of System. The content type ID of the Document content type is 0x0101, and the Folder content type has a content type ID of 0x0120. By parsing these content type IDs, you can determine that both Document and Folder are direct children of Item, which in turn inherits directly from System, as shown in the following figure.

Document content type ID

Using this convention, you can determine not only what content types a content type inherits from, but at which point two content types have common ancestors.

The following figure shows the relationship of the four content types discussed earlier. In each content type, the unique portion of the content type ID is represented by blue text.

Example of default content type ID hierarchy

Parent Content Type ID + "00" + Hexadecimal GUID

SharePoint Foundation uses this content type ID generation convention when you create content type IDs for the following:

  • Site content types you create based on other content types

  • List content types, which are copied to a list when you add a site content type to that list

For example, if you have a content type that has a content type ID of "0x010100D5C2F139516B419D801AC6C18942554D", you would know that the content type was one of the following:

  • A site content type that is a direct child of the Document content type

  • A list content type that was created when the Document site content type was added to a list

We recommend that you use the GUID approach to identify any content types that are direct children of content types you did not create. Use the GUID approach if the parent content type is one of the following:

  • A default content type included in SharePoint Foundation, such as Document.

  • A content type developed by a third party. That way, you are assured that the content type ID is unique and will not be duplicated later by the developer of the parent content type.

However, after you uniquely identify a content type using the GUID approach, you can use the first method to identify any children of that content type. The GUID used in your content type can act as a namespace for your content type. Any children based on that content type can be identified by just two hexadecimal digits. Because the maximum length of a content type ID is finite, this approach maximizes the number of allowed content type "generations."

Content Type ID Lengths

Content type IDs have a maximum length of 512 bytes. Because two hexadecimal characters can fit in each byte, each content type ID has an effective maximum length of 1024 characters.

For example, suppose you create a new content type, myDocument, based on the default SharePoint Foundation content type Document. For the myDocument content type ID, you start with the Document content type ID, 0x0101 and append 00 and a GUID. This uniquely identifies the myDocument content type, ensuring that SharePoint Foundation will not add another default content type later with the same content type ID (which is possible if you append only two hexadecimal digits). However, to generate content type IDs for any content types you derive from myDocument, you can just append two hexadecimal digits to the myDocument content type ID. This keeps the content type ID length to a minimum, thereby maximizing the number of allowed content type "generations."

The following figure shows this scenario. The unique portion of each content type ID is represented by blue text.

Example of content type ID hierarchy

Working with Content Type IDs in Declarative XML

You can create a new site content type as part of a Feature by using declarative XML in an element manifest file. SharePoint Foundation provides an XML schema definition for this purpose. For more information, see Content Type Definitions.

The top-level element in a content type definition is ContentType. This element has a required ID attribute whose value must be a string in content type ID format. If you do not correctly format the ID for a new content type, you will receive an error when you try to activate the Feature in which it is defined. To avoid this error, construct the ID for your new content type by carefully following the rules outlined in this topic.

To create an ID for a content type that is derived from a built-in content type, start with the hexadecimal string that identifies the built-in content type, append two zeros, and then add a GUID with all the punctuation removed. For example, the hexadecimal string that identifies the Item content type is "0x01". The ID for a content type that derives from Item should resemble the following:

0x0100A33D9AD9805788419BDAAC2CCB37509F

To create IDs for additional content types that inherit from your content type, just append "01", "02", and so on.

Tip

IDs for important built-in content types are listed in Base Content Type Hierarchy. In addition, you can quickly reference the ID for any content type that is available to a site by selecting Site Settings from the Server ribbon and then clicking Site content types in the Galleries section. When you move the mouse over the link for a content type, the content type ID is displayed in the browser's status bar.

The following example is an element manifest for a Feature that creates three content types. The first new content type, Financial Document, is derived from the Document content type. Financial Document, in turn, is the parent content type for new Purchase Order and Invoice content types.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">

  <!-- Parent: Document (0x0101) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e"
               Name="Financial Document"
               Group="Financial Content Types"
               Description="Base financial content type"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{1511BF28-A787-4061-B2E1-71F64CC93FD5}" Name="OrderDate" DisplayName="Date" Required="FALSE"/>
      <FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/>
    </FieldRefs>
  </ContentType>

  <!-- Parent: Financial Document (0x0101000728167cd9c94899925ba69c4af6743e) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e01"
               Name="PurchaseOrder"
               Group="Financial Content Types"
               Description="Used for creating purchase orders"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Item" Required="TRUE" Sealed="TRUE"/>
      <FieldRef ID="{943E7530-5E2B-4C02-8259-CCD93A9ECB18}" Name="CostCenter" DisplayName="Department" Required="TRUE"/>
    </FieldRefs>
    <DocumentTemplate TargetName="PurchaseOrder.docx"/>
  </ContentType>

  <!-- Parent: Financial Document (0x0101000728167cd9c94899925ba69c4af6743e) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e02"
                Name="Invoice"
                Group="Financial Content Types"
                Description="Used for creating customer invoices"
                Inherits="TRUE"
                Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Service" Required="TRUE" Sealed="TRUE"/>
      <FieldRef ID="{86811853-7E52-4515-A88D-A8FA9D450905}" Name="Client" DisplayName="Client Name" Required="TRUE"/>
    </FieldRefs>
    <DocumentTemplate TargetName="Invoice.docx" />
  </ContentType>
</Elements>

Working with Content Type IDs in Code

The value that is returned by the Id property of an SPContentType object in server code or by the same property of a ContentType object in client code is not a hexadecimal string. It is an object of type SPContentTypeId in server code and type ContentTypeId on the client.

If you know the hexadecimal string for a content type ID, you can get the corresponding object by passing the string to the constructor for the appropriate class. For example, the following server code constructs an SPContentTypeId object that represents the ID for the Document content type.

using (SPSite site = new SPSite(https://localhost))
{
   using (SPWeb web = site.OpenWeb())
   {
      // Get the Document content type.
      SPContentTypeId documentCTypeId = new SPContentTypeId("0x0101");
      SPContentType documentCType = web.AvailableContentTypes[documentCTypeId];

      // Do something.
      ...
   }
}

You do not have to know the hexadecimal ID for any of the content types that are included with SharePoint Foundation. Instead, you can use the fields of the SPBuiltInContentTypeId class. For example, wherever your code must represent the ID for the Document content type, it can use SPBuiltInContentTypeId.Document instead of "0x0101". The following code illustrates this technique.

using (SPSite site = new SPSite(https://localhost))
{
   using (SPWeb web = site.OpenWeb())
   {
      // Get the Document content type.
      SPContentType documentCType = web.AvailableContentTypes[SPBuiltinContentTypeId.Document];

      // Do something.
      ...
   }
}

When you create your own content type in code, you do not have to manufacture an ID for it. Instead, you can let the system generate an ID based on the ID of the content type from which your content type is derived.

For example, the following console application creates a new Customer content type that is derived from the Item content type. The application makes the new content type a site content type by adding it to the site's content type collection. Then it applies the content type to a list by adding it to the list's content type collection. Finally, the application prints the IDs for all three content types: the parent content type, the new site content type, and the new list content type.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Get a reference to the Item content type.
                    SPContentType parentCType = web.AvailableContentTypes[SPBuiltInContentTypeId.Item];

                    // Create a Customer content type derived from the Item content type.
                    SPContentType childCType = new SPContentType(parentCType, web.ContentTypes, "Customer");

                    // Add the new content type to the site collection.
                    childCType = web.ContentTypes.Add(childCType);

                    // Add it to a list.
                    SPList list = web.Lists["Announcements"];
                    list.ContentTypesEnabled = true;
                    SPContentType listCType = list.ContentTypes.Add(childCType);

                    // Print the IDs.
                    Console.WriteLine("Parent content type ID: {0}", parentCType.Id.ToString());
                    Console.WriteLine("Site content type ID:   {0}", childCType.Id.ToString());
                    Console.WriteLine("List content type ID:   {0}", listCType.Id.ToString());
                }
            }

            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

The following output is printed to the console:

Parent content type ID: 0x01
Site content type ID:   0x0100A33D9AD9805788419BDAAC2CCB37509F
List content type ID:   0x0100A33D9AD9805788419BDAAC2CCB37509F006454F8883E8BA648A632E5ACB47CAD9B

Press ENTER to continue...

See Also

Reference

SPContentTypeId

SPBuiltInContentTypeId

Concepts

Base Content Type Hierarchy

Site and List Content Types

Creating Content Types