Disclaimer: This post was in a draft state for many, many, months. It was started and subsequently turned into my Sitecore Symposium 2012 talk. Now that SitecoreSym is over I am making this post and the associated code available.
Sitecore has a robust API revolving around "Items" and "Fields". Sitecore's Item is a very heavy object with a lot of convenience methods and properties to things like 'Parent', 'Axes', 'Children', etc... A Sitecore Field is also very heavy with various versions for images, links, references, etc... Exposing these objects to clients wouldn't be ideal since they expose a lot of extras that are of no concern. Furthermore, trying to serialize these objects into JSON (the REST API serialization format of choice) would prove to be difficult if not impossible.
So we recognized that Sitecore falls a little short with trying to get content out. Here is what we need:
- “Sitecore Navigator”
- POCO content models
- Mapping framework
- Efficient JSON Serialization
- Service endpoint inside of Sitecore
The "Sitecore Navigator" is a trivial piece of the solution. It is simply a way by which we can get a Sitecore item with some unique identifier. This is almost as simple as Sitecore.Context.Database.GetItem(id);
POCO Content Models
bject (POCO) are objects unencumbered with inheritance or attributes needed for specific frameworks
. We want a set of these models, or classes, that represent our Sitecore templates. For each template we have we will have a corresponding .net class.
We need some method by which we can convert, or map, a Sitecore item into one of our POCO classes. Our weapon of choice is a convention based mapping framework. We will use reflection magic to determine what class to instantiate and populate based on a naming convention between Sitecore template and .net class.
We need to serialize our classes in order to server them over an API. JSON serialization is the way to go and ASP.NET Web API has it built in. Win!
In order for an external application to get Sitecore content out, the external application needs some endpoint to call. For our implementation we will use the newly released ASP.NET Web API. This allows us to define a route to a service controller. Our controller actions allow the applications to get content out.