Creating a REST data store in EPiServer 7 Preview

In EPiServer

This blog post is about how you create a REST data store in EPiServer 7 Preview.

The new REST data store in EPiServer 7 Preview

The new REST data store in EPiServer 7 Preview allows you much like the ASP.NET web API to register a handler for returning data in any format. It works much in the same fashion as the ASP.NET 4 Web API. Frederik Vig wrote a great article on using ASP.NET 4 Web API together with EPiServer CMS – go check it out if you haven’t!

With the REST data store you can register multiple stores with different purposes and output formats. What they have in common is that they can be called using the standard verbs of the HTTP protocol: GET, POST, PUT and DELETE.

JSON is the default return format when using the REST data store which is great for interoperability across different platforms.

REST data store registration

The REST data store is registered as an EPiServer CMS module. In order for our module to be discovered by the initialization framework we can add it to the modules-folder in our site root. See default configuration below.

The modules root directory is probed for modules when the autoDiscovery-attribute is set to Modules. We therefore add a folder for our module in the modules-folder of the site root. In this sample I’ll be creating a Star Wars store.

star-wars-store

In the module folder we add module.config where we have more granular control of our module. Our configuration will only contain which assembly should be used.

Implementing a REST data store

For our Star Wars module we’ll be creating two stores: Rebels and Empire. The class must inherit from RestControllerBase and be decorated with the RestStore-attribute in order for EPiServer to discover it during initialization. See sample code below.

The route(s) for REST data stores are registered with the following pattern: “~/modules/moduleName/stores/storeName”. Our Rebels-store will therefore be called with the URL “~/modules/starwars/stores/rebels”. The stores segment of the URL is always appended to the module-name when the route is registered.

In order to support the HTTP verbs of GET, PUT, POST or DELETE we add corresponding method-names. See sample code below for the GET-command.

There are two reserved parameters available when designing the methods: id and entity. The ID is the data you can append to the URL when for example when making a GET-request. E.g. http://yourwebsite/modules/starwars/stores/rebels/myIdData.

The entity is the posted item data. This data will be de-serialized into the parameter type specified.

Accessing the REST data store with a browser

The REST data store is now accessible via the GET HTTP verb from a browser. By calling the full path to the REST data store we get the data in JSON format.

star-wars-json

Note the “{}&&” characters that prefix the JSON-data to prevent JSON hijacking.

REST data store usage

I’m sure there will be many innovative ways of using the REST data store in EPiServer 7. The things I can come up with at the top of my mind are for integration with Dijit Widgets for the UI as well as integration with other application. Combining the REST data store with the Dynamic Data Store can provide some powerful and flexible solutions for working with other data than that directly associated with pages.

Thanks for reading!

daniel
daniel
Developer
Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search