Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Sitecore Services Client ServicesApiController

With Sitecore 7.5 and now Sitecore 8, Sitecore.Services.Client (SSC) was introduced. This has given us a flexible and powerful framework for created Web API based rest services in Sitecore (See my introduction into Entity Service (part of Sitecore.Services.Client))

Entity Service is powerful, offering the repository pattern to implement functionality such as FindAll, FindByID, Create, Delete and Update for a given Entity (Model).

However, what happens if you want to create a completely bespoke Web API controller and don't need any of the functionality from the repository of Entity Service. Up until now we have done something similar to my blog article here where we extend some Sitecore pipelines to register custom routes and abort those.

ServicesApiController

With Sitecore.Services.Client we now have another option available to us, ServicesApiController. ServicesApiController is a abstract class that sits on-top of the .NET ApiController, offering the same core functionality as the start controller. ServicesApiController sits within Sitecore.Services.Infrastructure so you need to make sure you include the DLL in your solution.

The advantage of using ServicesApiController means we get all the security benefits and filters of Sitecore.Service.Client Anders Laub has a blog post talking about this.

ServicesController

If we don't want to extend the Sitecore pipelines to register routes for our custom controller, we can use another part of Sitecore.Services.Client - ServicesController. ServicesController is a attribute we can use to decorate our controllers, this then includes the controller is Sitecore.Services.Client routing.

Decorating our controllers with this attribute means we no longer need to extend any of the Sitecore pipelines to configure the routing. Our custom controller will now use the Sitecore.Services.Client style routing my default.

Eg. //HostName/sitecore/api/ssc/namespace-to-controller/{controller}/{id}/action.

Below is an example of a custom controller using ServiceApiController together with ServicesController.

Here is the output from a web request to the controller action.

JSON returned

JSON returned