Compartir a través de


SPContentType.Id Property

Gets an SPContentTypeId object that represents the content type ID of the content type.

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

Syntax

Public ReadOnly Property Id As SPContentTypeId
    Get

Dim instance As SPContentType
Dim value As SPContentTypeId

value = instance.Id
public SPContentTypeId Id { get; }

Property Value

Type: Microsoft.SharePoint.SPContentTypeId
Returns SPContentTypeId.

Remarks

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. By parsing the content type ID, you can determine which content types the content type inherits, and how two content types are related.

For more information, see Content Type IDs.

Examples

The following example is a console application that searches a site for lists that have the built-in Document content type in their content type collection. When a match is found, the application prints the parent content type’s Id and the matching content type’s Id to the console.

Imports System
Imports Microsoft.SharePoint

Module Test
   Sub Main()
      Using site As SPSite = New SPSite("http://localhost")
         Using web As SPWeb = site.OpenWeb()

            ' Search for a built-in content type on lists in the site.
            Dim parentId As SPContentTypeId = SPBuiltInContentTypeId.Document

            For Each list As SPList In web.Lists
               Dim matchId As SPContentTypeId = _
                  list.ContentTypes.BestMatch(parentId)
               ' Report a match only if the list is not hidden
               ' (e.g. Master Page Gallery).
               If parentId.IsParentOf(matchId) And Not list.Hidden Then
                  Console.WriteLine("{0} has the {1} content type.", _
                                     list.Title, _
                                     list.ContentTypes(matchId).Name)
                  Console.WriteLine("parent content type id: {0}", _
                                     parentId.ToString())
                  Console.WriteLine("  list content type id: {0}",_
                                     matchId.ToString())
               End If
            Next list
         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Search for a built-in content type on lists in the site.
               SPContentTypeId parentId = SPBuiltInContentTypeId.Document;
               foreach (SPList list in web.Lists)
               {
                  SPContentTypeId matchId = 
                     list.ContentTypes.BestMatch(parentId);
                  // Report a match only if the list is not hidden 
                  // (e.g. Master Page Gallery).
                  if (parentId.IsParentOf(matchId) && !list.Hidden)
                  {
                     Console.WriteLine("{0} has the {1} content type.",
                                        list.Title, 
                                        list.ContentTypes[matchId].Name);
                     Console.WriteLine("parent content type id: {0}",
                                        parentId.ToString());
                     Console.WriteLine("  list content type id: {0}", 
                                        matchId.ToString());
                  }
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

The application prints the following output to the console.

Shared Documents has the Document content type.
parent content type id: 0x0101
  list content type id: 0x010100C21DAAD3BCD4FF409A4DB5005BF7E12F

Press ENTER to continue...

See Also

Reference

SPContentType Class

SPContentType Members

Microsoft.SharePoint Namespace

Other Resources

Content Type IDs

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy

SPContentType Object Overview