Tutorial: Create a RESTful WCF service to retrieve titles of news items
This tutorial demonstrate how to develop a Sitefinity RESTful WCF service that retrieves the titles of all news items.
The tutorial contains the following sections:
- Prepare the project
- Implement the service interface
- Implement the service class
- Register the service
- Using the service
Prepare the project
- In Visual Studio, open your Sitefinity CMS project.
- In the context menu of your project, click Add » New Folder.
- Name the new folder CustomServices.
- Open the context menu of the CustomServices folder and click Add » New item… » Interface.
- Name the interface INewsService.cs.
- Open the context menu of the CustomServices folder and click Add » New item… » Class.
- Name the class NewsService.cs.
- Open the context menu of your project and click Add » New item… » Global Application Class.
- Name the class Global.asax.
Implement the service interface
Open the INewsService interface and add the following code:
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace SitefinityWebApp.CustomServices
{
[ServiceContract]
public interface INewsService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
IQueryable<string> GetNewsTitles(int? startIndex, int? take);
}
}
In the code above, you first mark the interface with the ServiceContract attribute, which defines the interface as a service contract. Next, you expose the GetNewsTitles method as a web service method by adding the OperationContract attribute. Finally, you add the WebInvoke attribute, which is used to define how the service operation is called.
Implement the service class
Open the NewsService class and add the following code:
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Telerik.Sitefinity.Modules.News;
namespace SitefinityWebApp.CustomServices
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class NewsService : INewsService
{
public IQueryable<string> GetNewsTitles(int? startIndex, int? take)
{
var newsManager = NewsManager.GetManager();
var newsTitles = newsManager.GetNewsItems().ToList().Select(x => x.Title.ToString());
if (startIndex != null)
{
newsTitles = newsTitles.Skip((int)startIndex);
}
if (take != null)
{
newsTitles = newsTitles.Take((int)take);
}
return newsTitles.AsQueryable();
}
}
}
In the code above, you add the ServiceBehavior attribute, which specifies the internal execution behavior of the service contract implementation. Next, you add the AspNetCompatibilityRequirements attribute, which indicates whether a service can be run in ASP.NET compatibility code. Finally, you inherit the INewsService interface and implement the code to return all the news item titles.
Register the web service
Open the Global.asax file and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Telerik.Sitefinity.Services;
namespace SitefinityWebApp
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
SystemManager.ApplicationStart += new EventHandler<EventArgs>(ApplicationStartHandler);
}
private void ApplicationStartHandler(object sender, EventArgs e)
{
SystemManager.RegisterWebService(
typeof(SitefinityWebApp.CustomServices.NewsService),
"CustomServices/NewsService");
}
}
}
In the code above, you use the SystemManager class to execute the ApplicationStartHandler method upon application start of the Sitefinity CMS project. Inside the ApplicationStartHandler method, you register the dynamic WCF RESTful service.
Use the service
Once you implement and register the Sitefinity CMS RESTful WCF service, you can execute the service using a jQuery AJAX call in any of your Sitefinity CMS widgets or pages. To do this, use the following code:
$.ajax({
type: "POST",
url: "CustomServices/NewsService/GetNewsTitles",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ skip: 0, take: 10 }),
success: function (result) { console.log(result); },
error: function (error) { alert(error); }
});