ItemContentType Implementation

A good example is to look at the ItemContentType and ItemContentTypeDto implementations.

The implementation of an ItemContentType (representing the Item SPContentType) is as follows:

    public class ItemContentType : BaseContentType, IContentType<ItemContentTypeDto>
        #region FieldTypes

        public static ContentTypeIdFieldType ContentTypeId
            get { return new ContentTypeIdFieldType(SPBuiltInFieldId.ContentTypeId); }

        public static SimpleFieldType<string> Title
            get { return new SimpleFieldType<string>(SPBuiltInFieldId.Title); }


        #region Methods

        public new ItemContentTypeDto Get(object item)
            BaseContentTypeDto baseDto = base.Get(item);

            var spListItem = item as SPListItem;

            var contentTypeId = ContentTypeId.GetValue(spListItem);
            var title = Title.GetValue(spListItem);
            var itemContentTypeDto = new ItemContentTypeDto(contentTypeId, title, 

            return itemContentTypeDto;

        public void Update(object item, ItemContentTypeDto dto)
            base.Update(item, dto);

            var spListItem = item as SPListItem;  
            ContentTypeId.SetValue(spListItem, dto.ContentTypeId);
            Title.SetValue(spListItem, dto.Title);


        #region ContentTypeId

        public override string GetContentTypeIdDefinition()
            return SPBuiltInContentTypeId.Item.ToString();


You will notice there are 4 main members to this class.
  • Public FieldType properties with Get accessor
    • These FieldTypes represent the fields specific to the Item SPContentType that it represents.
    • They are read-only because they are mereley a definition of the content type and this cannot change at run time (but can however change at desing time).
    • They instantiate themselves by creating the FIeldType with a FieldId, connecting themselves to a specific field on the SPListItem they will interact with.
  • GetContentTypeIdDefinition method
    • Returns the SPContentTypeId of the Item SPContentType that it will represent.
    • Do not mistake this with the ContentTypeIdFieldType that exposes the run time value of the SPContentTypeId
  • Methods
    • Get method
      • Has an SPListItem parameter on which it will act
      • Will return the corresponding ContentTypeDto
      • Will initialize each ContentTypeDto's property by calling the corresponding FieldType.GetValue method
    • Update method
      • Ditto Get Method but instead of calling GetValue it will perform a SetValue acting on the provided SPListItem

As you may notice, the SharePoint object SPListItem is used in this class. This is not a problem because we are implementing definitions for SharePoint objects (SPListItem objects). If you have to create a repository for a different datasource than SharePoint, you would represent those objects with different IContentType implementations, which always act on objects.

Also notice that the ItemContentType inherits from BaseContentType (which is an implementation that represents the system fields like modified, modifiedby, created, createdby...) but also re-implements IContentType<ItemContentTypeDto>. This is necessary to allow to return and accept the corresponding ContentTypeDto for this ContentType implementation.

Last edited Nov 1, 2013 at 11:30 AM by cverhelst, version 8