Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Sitecore Entity Service (SSC) Custom Controller Action

Following on from my post on the basics of Entity Service and using the JavaScript API (Entity Service in Sitecore.Service.Client), in this post I will explain how to extend Entity Service Sitecore.Services.Client (SSC) with new controller actions.

Using Entity Service as standard you get the following methods:

  • IQueryable GetAll()
  • Entity FindById(string id)
  • Add(Entity entity)
  • Exists(Entity entity)
  • Update(Entity entity)
  • Delete(Entity entity)

However Entity Service is very flexible allowing you to extend the service controller with additional actions. To add an additional action it is as simple as adding a new function to the service controller, and decorating it with an ActionName and the action type such as HttpGet. Please note the below of the required routing for Entity Service custom actions.

With this method you will be implementing the functionality of the action in the controller or in some other business logic class. However I like the repository pattern that Sitecore have put in place with Entity Service using the IRepository interface. I would rather keep all the logic of my controller in a single place (in the repository).

So how do we update the repository to include the extra functional we want to add to the Entity Service.

Firstly I created a new interface which contains my new funcations. In this example I have created the new DoSometingCustom() function that returns a string. To make sure this new repository works with the Service Controller and Entity Service I inherit from the default repository of IRepository, making sure T must implement IEntityIdentity.

Now you are able to update the repository class and instead of implementing IRespository implement the new interface instead. This then gives you all the functions of the base IRepository interface (with standard Entity Service functions) along with your new interface with custom functions.

The last step is to update your controller class. Instead of creating an instance of your IRepository in the constructor of your controller, create an instance of your custom repository interface.

From here you can then add your additional controller actions, calling the repository to implement the functionality of the controller.

Please Note

With custom controller actions in Entity Service, the routing is configured to respond to {Controller-Namespace}/{Controller}/{ID}/{CustomActionName}. If you do not include in an ID  in the URL, Entity Service will not resolve to your custom controller action. If you do not require passing an ID to your custom controller action you can provide an int but not process the ID. This will satisfy the routing for Entity Service.

As seen below you can then call your controller by using the prefixed of "/sitecore/api/ssc". The next part of the url is the namespace containing your controller with the "." replaced with "-" followed by your controller name.

E.g. If you your controller is in the namespace MikeRobbins.EntityServiceCustomActions.Controllers and called EntityController. Your url would be "/sitecore/api/ssc/MikeRobbins/api/ssc/MikeRobbins-EntityServiceCustomActions-Controllers/Entity/{ID}/{CustomActionName}"

Entity Service Custom Controller Action

Entity Service Custom Controller Action

Demo Application Source Code

I have created a demo application using this method. The source is available on GitHub.