Posted: 6/21/2020
Building websites with Sitecore CMS is largely about creating components that will be used on the website (so-called renderings). Besides of creating controller renderings to handle components of your site, sometimes you need to prepare an API, e.g. for integration with external services. In today’s post I’ll show you how to create API controller in Sitecore XP 9.3 and how to register it using dependency injection.
Prerequisities
- Start with creating new project (class library) in your solution - depending on the purpose of the project in the Feature, Foundation or Project layer
- Install following packages:
- Microsoft.Extensions.DependencyInjection
- Newtonsoft.Json 11.0.2 - if you want to return items serialized to JSON in your api controller
Create controller
Add new folder to the project and name it Controllers. Add new class with your controller’s name (remember about Controller suffix). Class should inherits from ApiController.
Next, create a method for your API endpoint. If you want to change the default .NET routing, you can add Route attribute (like in the example below).
namespace Feature.ExampleApi.Controllers
{
public class ExampleApiController : ApiController
{
[HttpGet]
[Route("get/{itemIdString}")]
public IHttpActionResult GetItem(string itemIdString)
{
if (!ID.TryParse(itemIdString, out var itemId)) return null;
var item = Sitecore.Context.Database.GetItem(itemId);
if (item == null) return NotFound();
// Do sth with item
return Json(result);
}
}
}
The method above attempts to obtain the item from Sitecore database based on its ID. If succeed, you can work with this item - edit, adapt to your DTO model or just return value of item’s property.
Register Controller
Newly created controller should be registered in DI container. There are many articles on how to do it properly - like this official doc.
So much has been written about dependency injection in Sitecore that one could successfully write a book on this subject. One of the best summaries in this topic which I’ve found was written by Corey Smith - you can read it here Sitecore Dependency Injection - Scoped Services.
Here’s the way I personally use - it seems to me the most convenient. Just create ServicesConfigurator.cs file and add following content (remember about usings!):
namespace Feature.ExampleApi
{
public class ServicesConfigurator : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
// Controllers
serviceCollection.Replace(ServiceDescriptor.Transient(typeof(ExampleApiController),
typeof(ExampleApiController)));
}
}
}
Create config files
You also need to register your ServicesConfigurator class to be attached to Sitecore config. In order to do that, create Feature.ExampleApi.config file under App_Config/Include/Features path (where ExampleApi is the name of your project):
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<services>
<configurator type="Feature.ExampleApi.ServicesConfigurator, Feature.ExampleApi" />
</services>
</sitecore>
</configuration>
Et voila! After publishing your controller is ready for work. It’s available at the following endpoint:
{instance_url}/get/{item_id}
Useful links:
- Controller rendering in Sitecore with example
- Dependency Injection - Sitecore Documentation
- Sitecore Dependency Injection - Scoped Services
Hero photo by Rachel Houghton on Unsplash.