Sitecore.Services.Client provides a powerful and flexible method of creating Web API based services within Sitecore. Using the provided interfaces and the repository pattern means that you will be building services in a consistent way, allowing you to build more flexible services and reduce the time to develop and maintain them. Sitecore.Services.Client was introduced first in Sitecore 7.5 and supported also in Sitecore 8.
Sitecore.Services.Client consists of two separate services. ItemService which allows you to work with standard Sitecore Item class. The other is EntityService which allows you to work with business entities that you create, and allows you implement the business logic as you require.
The first stage of creating a EntityService based service is to create a Model. This model will define your business entity you wish to work with. All Models have to implement the Sitecore.Services.Core.Model.EntityIdentity class.
Create a repository based on the IRespository interface. E.g Sitecore.Services.Core.IRepository
IRepository will give you methods for Add, Delete, Exists, FindById, GetAll and Update of your Model. You can however extend this if you require.
Next create a controller and add the ServicesController attribute. Implement the EntiryService
E.g. If you your controller is in the namespace MikeRobbins.News.Controllers and called NewsArticleController. Your url would be "/sitecore/api/ssc/MikeRobbins/api/ssc/MikeRobbins-News-Controllers/newsarticlearticle"
Get Entities Example
Create an instance of EntityService with the service url, you can then call fetchEntities. This will then call the GetAll method within your repository defined within the controller. An array of the entities are then returned.
Get Entity Example
Create an instance of EntityService with the service url, you can then call the fetchEntity function passing in an id of an entity. This will then call the FindById method within your repository defined within the controller. An entity class in returned.
Update Entity Example
Create a an instance of EntityService with the service url, you can then call the fetchEntity function passing in an id of an entity. Once you have an entity returned, make the required changes to the entity. From here you can call the save method, this maps to the update method within the repository. A class is returned containing the updated entity, in the below example I'm popping up a success message in the SPEAK message panel.
Delete Entity Example
Create an instance of EntityService with the service url, you can then call the Delete function passing in the entity to delete. This will then call the delete method within your repository defined within the controller.
Create Entity Example
Create an instance of EntityService with the service url. You can then create a new object populating the properties of your entity. From here you can call CreateEntity, passing in the object you created. This will then call the add method within your repository defined within the controller. A class is returned of the newly created entity.
There is a few tools you can use to help with debugging and exploring SSC.
- Fiddler http://www.telerik.com/fiddler
- Kevin Obee's Sitecore Glimpse Plugin. Under Sitecore Tab there is a sitecore services section where you can explore your SSC services.
- POSTman https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en
- Chrome's Console
Jamie Little has written a useful post on how to add custom Authorisation Filters to the controller to restrict access.
I have an example Sitecore SPEAK application with SSC implemented available on GitHub. https://github.com/komainu85/SitecoreSPEAKBulkWorkflows/