Business Entities and Repositories

Entities in the Framework are explicit implementations of the contenttypes of your system providing not only type-safe access to their data but also a separation of your business processes.

SharePoint Standard Types

SharePoint provides a set of content types like Item, Contact, Event, ... for its standard features. Currently the namespace provides the classes Item, Contact, Event and Task.
You can, and you should, inherit these types for your content types if they are a subtype of the corresponding content type.


A typical entity would look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

    [ContentType(CTYPE_ID)] // mark the class with its content type
    public class Employee : Contact
        #region Constants
        public new const string CTYPE_ID = "0x01060094b52f1082474c80a0383212873c85af";
        public new static readonly SPContentTypeId CTypeId = new SPContentTypeId(CTYPE_ID);
        public const string ListUrl = "Lists/Employees"; // frequently needed to find the employees list

        public const string LOOKUP_DEPARTMENT = "fLookupDepartment";

        #region Properties
        public Department Department
            get { return GetLookup<Department>(LOOKUP_DEPARTMENT); }
            set { SetLookup(LOOKUP_DEPARTMENT, value); }

        #region Constructor
        public Employee(int id, string title) : base(id, title) { }
        public Employee(int id, string title, SPList list) : base(id, title, list) { }
        public Employee(SPItemEventProperties eventProperties, SPWeb web) : base(eventProperties, web) { }
        public Employee(SPListItem listItem) : base(listItem) { }
        public Employee(SPItemEventProperties eventProperties, SPListItem listItem, SPWeb web) : base(eventProperties, listItem, web) { }


A ListRepository is to a SPList the same as an entity to a SPListItem. It wraps the functionality of the list object and extends it with commonly used methods and properties.

Fetch Entities

Instead of retrieving a SPListItem from a list and converting it to an entity using the ListItemMapper, one can use the GetEntity<TEntity>() method to get the entity directly from the repository.

var repository = new ListRepository("lists/Employees", SPContext.Current.Web); // creates a repository for the list at 'lists/Employees
var entity = repository.GetEntity<Employee>(17); // returns the employee with the id '17' from the list

Other GetEntities<TEntity>() methods fetch many entities using an optional query, content type or view scope. You'll get all entities of the list if you omit these arguments.

var repository = new ListRepository("lists/Employees", SPContext.Current.Web);

var entities1 = repository.GetEntities<Employee>(); // returns all items from the list
var entities2 = repository.GetEntities<Employee>(SPViewScope.RecursiveAll); // uses a scope to find items in the list
var entities3 = repository.GetEntities<Employee>(new SPContentTypeId("0x0100...")); // returns only items of a certain content type
var entities4 = repository.GetEntities<Employee>(new SPQuery().Where(QB.Eq("LastName", "Korn"))); // queries the list and returns the result

Create Entities

To create new listitems use the Create<TEntity>() methods of the repository.

var repository = new ListRepository("lists/Employees", SPContext.Current.Web);

var employee = repository.Create<Employee>(); // creates a new listitem an returns its entity
employee.LastName = "Korn";
employee.FirstName= "Steve";
employee.Update(); // by calling the Update method the item gets stored in the content database

Last edited Jul 8, 2011 at 8:04 AM by florianwachter, version 11


No comments yet.