Out of the box Field and ContentType IDs

SharePoint keeps amazing me, even after working more than 6 years with the product. The product is this big that’s it is almost impossible to know all. This is one of those things. I simply did see it before.

When working on a “Yet Another SharePoint Gem” as Robin Meure called it. I stumbled on the SPBuiltInFieldId class and SPBuiltInContentTypeId class. When retrieving information like the CreatedBy field (also known as Author) it is very useful. Also when checking whether a item is based on a out-of-the-box (OOTB) content type. See exaples below.

Update: in addition to both SharePoint 2010 Foundation classes SharePoint Server has ContentTypeId class and FieldId class. Both are part of the Microsoft.SharePoint.Publishing namespace. Thanks to Jeffrey Schmitz (@DJeffa).

Here an example for the extension of the SPListItem class to retrieve the Author:

public static class SPListItemExtentions { /// <summary> /// Gets the author of a list item. /// </summary> /// <param name="item">Extended class.</param> /// <returns>Returns the author of the list item.</returns> public static SPUser GetAuthor(this SPListItem item) { string user = item[SPBuiltInFieldId.Author].ToString(); int userId = Convert.ToInt16(user.Split(';')[0]); return item.Web.AllUsers.GetByID(userId); } }

Another example for checking the base content type:

SPListItem item = list.GetItemById(1); SPListItem topic = item; // If item isn't the dicussion topic, get topic! if (!item.ContentType.Id.IsChildOf(SPBuiltInContentTypeId.Discussion)) topic = GetTopicItem(item);

Leave a comment

1 Comment

  1. Besides the ones you named, there are also the classes Microsoft.SharePoint.Publishing.ContentTypeId and
    Microsoft.SharePoint.Publishing.FieldId. The function is the same, but are ment for the contenttypes and fields added by the Publishing features


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: