SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Lesson 2 – Content Types – Part II

The first step in creating content types is the choice of a built-in type. A built-in type identifies the source and how two content types are related. This post is a continuation of Lesson 2 - Content Types, in which I will explain Content Type IDs and show some examples about the creation of IDs.

Hi folks,

The first step in creating content types is the choice of a built-in type. A built-in type identifies the source and how two content types are related.

This post is a continuation of Lesson 2 - Content Types, in which I will explain Content Type IDs and show some examples about the creation of IDs.

For those who like to check in details how things work, I recommend this reading!

Content Type IDs

There are some rules to create IDs for content types with the use of XML files.

The first step in creating content types is to choose a built-in type, which are predefined types of content types. Figure 1 shows some examples:

1
Figure 1 - Examples of Built-in Content Types

Each built-in type has a unique identification, indicating which built-in type it inherited from and how they are related.

By analyzing the figure above, observe the hierarchical structure of the identification of built-in types. In blue colour we have the identifiers of inherited built-in types and, in black colour is the built-in type of which it was derived. This represents a tree structure of inheritance.

Built-in and Custom Content Type IDs

There are two ways of creating content type IDs:

  • Parent content type ID + 2 hexadecimal values
    NOTE: Designed to identify a base built-in content type

2
Figure 2 – Built-in Content Type ID

In the example above the selected block in red colour always changes, whose 2 hexadecimal values cannot be "00". The last blank block does not apply in this case.

  • Parent content type ID + "00" + hexadecimal GUID.
    NOTE: Designed to identify a custom content type.

3
Figure 3 – Custom Content Type ID

In the example above the block of Figure 2 remains at "00", and the selected block in red colour must receive a GUID.

Internally, the inheritance scheme is interpreted by SharePoint in blocks (Figures 2 and 3), which facilitates the identification of the ancestor of the content type.

Base Content Type Hierarchy

Both SharePoint 2007 and SharePoint 2010 Foundation have a base content type hierarchy, in which all the content types are derived. Consider the Figure 4:

4
Figure 4 – Base Content Type Hierarchy

As you can see, all content types inherit from System. This content type is the sealed type and can not be edited.

The content types that belong to the group _Hidden are not displayed in the User Interface. They are for internal use of SharePoint.

You can assign content types to list items, documents, and folders. Content types that inherit from the Document site content type can be assigned only to libraries. Similarly, you can assign content types that inherit from the Item site content type only to lists. Content types that inherit from the Folder content type template can be assigned to either libraries or lists, because both can contain folders.

Obtaining Base Content Type Hierarchy Programmatically

Through a recursive algorithm, that uses the LinkedList object to create the hierarchical tree, I simulate such reading.

This is a code for purposes of exemplification only, widely used in my trainings. The code consists of 3 classes:

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6.  
  7. namespace BuiltinContentTypes
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             using (SPSite site = new SPSite("http://localhost/sites/team"))
  14.             {
  15.                 using (SPWeb web = site.RootWeb)
  16.                 {
  17.                     foreach (SPContentType ct in web.AvailableContentTypes)
  18.                     {
  19.                         Common.CreateContentTypeTree(web, ct.Id);
  20.                         Common.Level = 1;
  21.                     }
  22.                 }
  23.             }
  24.             Common.DisplayContentTypeTree();
  25.             Console.ReadLine();
  26.         }
  27.     }
  28. }

Class Program - Responsible for the execution of code

 

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6.  
  7. namespace BuiltinContentTypes
  8. {
  9.     class ContentMapping : IEqualityComparer<ContentMapping>
  10.     {
  11.         #region Properties
  12.  
  13.         public string Name { get; set; }
  14.     ��   public SPContentTypeId Id { get; set; }
  15.         public int Level { get; set; }
  16.  
  17.         #endregion
  18.  
  19.         #region IEqualityComparer<ContentMapping> Members
  20.  
  21.         public bool Equals(ContentMapping x, ContentMapping y)
  22.         {
  23.             return x.Id == y.Id;
  24.         }
  25.  
  26.         public int GetHashCode(ContentMapping obj)
  27.         {
  28.             return obj.GetHashCode();
  29.         }
  30.  
  31.         #endregion
  32.     }
  33. }

Class ContentMapping - Responsible for storing data of content types

 

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6.  
  7. namespace BuiltinContentTypes
  8. {
  9.     class Common
  10.     {
  11.         #region Variables
  12.  
  13.         static int level = 1;
  14.         static LinkedList<ContentMapping> mapping = new LinkedList<ContentMapping>();
  15.         static ContentMapping content = null;
  16.  
  17.         #endregion
  18.  
  19.         #region Properties
  20.  
  21.         public static int Level
  22.         {
  23.             get { return level; }
  24.             set { level = value; }
  25.         }
  26.  
  27.         #endregion
  28.  
  29.         #region Methods
  30.  
  31.         /// <summary>
  32.         /// Creates a ContentMapping tree
  33.         /// </summary>
  34.         /// <param name="web">Sharepoint web</param>
  35.         /// <param name="ctId">ContentTypeId</param>
  36.         public static void CreateContentTypeTree(SPWeb web, SPContentTypeId ctId)
  37.         {
  38.             var contentType = web.AvailableContentTypes[ctId];
  39.  
  40.             // recursive
  41.             if (ctId.ToString() != "0x") CreateContentTypeTree(web, contentType.Parent.Id);
  42.  
  43.             if (level == 1)
  44.             {
  45.                 var cm = new ContentMapping() { Id = ctId, Name = contentType.Name, Level = level };
  46.                 if (mapping.Count == 0)
  47.                 {
  48.                     mapping.AddFirst(cm);
  49.                     content = cm;
  50.                 }
  51.             }
  52.             else
  53.             {
  54.                 var cm = new ContentMapping() { Id = ctId, Name = contentType.Name, Level = level };
  55.                 if (!mapping.Contains(cm, cm))
  56.                 {
  57.                     mapping.AddAfter(mapping.Find(content), cm);
  58.                     content = cm;
  59.                 }
  60.             }
  61.   ��         level++;
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Displays the tree
  66.         /// </summary>
  67.         public static void DisplayContentTypeTree()
  68.         {
  69.             StringBuilder tab = new StringBuilder();
  70.  
  71.             foreach (var item in mapping)
  72.             {
  73.                 tab.Remove(0, tab.Length);
  74.                 for (int i = 0; i < item.Level; i++)
  75.                     tab.Append("   ");
  76.                 if (item.Id.ToString().Length < 15) // just getting base built-in types
  77.                     Console.WriteLine(tab.ToString() + item.Id + " - " + item.Name);
  78.             }
  79.         }
  80.  
  81.         #endregion
  82.     }
  83. }

Common Class - Helper class that provides methods for construction of the tree

Download the code here.

At the end of execution, the content displayed on the console must be the same in Figure 4 (if you did not change any content type).

5
Figure 5 – Console of base content type hierarchy

Finally we can reproduce the image in Figure 4 with some additional built-in types that are not contained in the documentation of SharePoint 2010 (probably because the current documentation is preliminary and subject to change).

Well folks, that's it.

References:
http://msdn.microsoft.com/en-us/library/ms452896.aspx
http://msdn.microsoft.com/en-us/library/ms452896(office.14).aspx
http://msdn.microsoft.com/en-us/library/aa543822.aspx
http://msdn.microsoft.com/en-us/library/aa543822(office.14).aspx
Book: Professional SharePoint 2007 Development (WROX)

Cheers,

Marcel Medina

blog comments powered by Disqus