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:
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
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.
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:
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
-
- namespace BuiltinContentTypes
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (SPSite site = new SPSite("http://localhost/sites/team"))
- {
- using (SPWeb web = site.RootWeb)
- {
- foreach (SPContentType ct in web.AvailableContentTypes)
- {
- Common.CreateContentTypeTree(web, ct.Id);
- Common.Level = 1;
- }
- }
- }
- Common.DisplayContentTypeTree();
- Console.ReadLine();
- }
- }
- }
Class Program - Responsible for the execution of code
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
-
- namespace BuiltinContentTypes
- {
- class ContentMapping : IEqualityComparer<ContentMapping>
- {
- #region Properties
-
- public string Name { get; set; }
- public SPContentTypeId Id { get; set; }
- public int Level { get; set; }
-
- #endregion
-
- #region IEqualityComparer<ContentMapping> Members
-
- public bool Equals(ContentMapping x, ContentMapping y)
- {
- return x.Id == y.Id;
- }
-
- public int GetHashCode(ContentMapping obj)
- {
- return obj.GetHashCode();
- }
-
- #endregion
- }
- }
Class ContentMapping - Responsible for storing data of content types
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
-
- namespace BuiltinContentTypes
- {
- class Common
- {
- #region Variables
-
- static int level = 1;
- static LinkedList<ContentMapping> mapping = new LinkedList<ContentMapping>();
- static ContentMapping content = null;
-
- #endregion
-
- #region Properties
-
- public static int Level
- {
- get { return level; }
- set { level = value; }
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Creates a ContentMapping tree
- /// </summary>
- /// <param name="web">Sharepoint web</param>
- /// <param name="ctId">ContentTypeId</param>
- public static void CreateContentTypeTree(SPWeb web, SPContentTypeId ctId)
- {
- var contentType = web.AvailableContentTypes[ctId];
-
- // recursive
- if (ctId.ToString() != "0x") CreateContentTypeTree(web, contentType.Parent.Id);
-
- if (level == 1)
- {
- var cm = new ContentMapping() { Id = ctId, Name = contentType.Name, Level = level };
- if (mapping.Count == 0)
- {
- mapping.AddFirst(cm);
- content = cm;
- }
- }
- else
- {
- var cm = new ContentMapping() { Id = ctId, Name = contentType.Name, Level = level };
- if (!mapping.Contains(cm, cm))
- {
- mapping.AddAfter(mapping.Find(content), cm);
- content = cm;
- }
- }
- level++;
- }
-
- /// <summary>
- /// Displays the tree
- /// </summary>
- public static void DisplayContentTypeTree()
- {
- StringBuilder tab = new StringBuilder();
-
- foreach (var item in mapping)
- {
- tab.Remove(0, tab.Length);
- for (int i = 0; i < item.Level; i++)
- tab.Append(" ");
- if (item.Id.ToString().Length < 15) // just getting base built-in types
- Console.WriteLine(tab.ToString() + item.Id + " - " + item.Name);
- }
- }
-
- #endregion
- }
- }
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).
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