EPiServer Template Foundation WCF REST JSON service, part two

In EPiServer

This blog series is about extending EPiServer Template Foundation (ETF) with a WCF service that can be accessed with REST and returns JSON. This is the second part in a series of three. The first part is available here.

Creating the entities

For brevity I’ve only created two simple entities: Article and Contact. These two classes contain mirrored properties from their ETF conterparts.

Now we have two simple entities to play around with. Let’s move on to actually creating these using an entity factory.

Creating the entity factory

For the post I’ll only include the methods for creating Article. You can download the source code to see the full code.

We start off by creating a static ETFServiceEntityFactory-class with the following static methods.

We’re using the PageFactory class in ETF to retrieve all articles. Observe that GetArticles in PageFactory has a fair number of overloads which allows you to specify language, maximum number of articles and to include unpublished articles. In our example we’ll use the overload which takes no parameters and the overload with maxCount.

As you see we use a static method called CreateArticle and passing in the PageData object cast to the type of ArticlePageBase. Let’s go ahead and define this static method as well.

Create the methods for GetContacts and CreateContact as well using the same design.

Creating the WCF service

Add a new WCF Service to the Services folder in the project. You may want to remove the additional Services name in the namespace that gets added. E.g. DBLOG.EPiCMS6Sandbox.Services.Services into DBLOG.EPiCMS6Sandbox.Services.

First off we need to define the methods available in the service contract.

Using two operation contracts with the same method name is not allowed. So either we change the method name or set the Name-property of the operation contract. I opted for the second solution.

We’ve also added an attribute called WebGet which indicates that the method can be called using REST. The UriTemplate defines how the method is called with REST. In our example the request will look like this:

  • http://<someUrl>/ourService.svc/Articles
  • http://<someUrl>/ourService.svc/Articles/20

The response format tells the method that the response should be in the format of JSON. Finally we implent the interface in our service.

happinessNow that we’ve created our ETF WCF REST JSON (acronym win!) service we just need to create some sample data in our EPiServer website and take it for a test run. On to part three!

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