Tags

Sitecore manage content very neatly in the form of tree. You can manage your organization data easily using Sitecore. Your data need to be served to the broad range of clients including web browsers, mobile, different mobile application and other integrated services of your website. The Restful API is the way to fulfill these requirements. The output can be configured in JSON or XML format.

In this post you can learn how you can create and use REST API to expose Sitecore data for your different customers and users.

1. I have created some bios in Sitecore as below

Bios

2. Let us add an interface “IBioInfo.cs”

using System.ServiceModel;
using System.ServiceModel.Web;
using CommonLibrary;

namespace SitecorePOC
{
[ServiceContract]
public interface IBioInfo
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "?ID={BioID}&All={All}")]
BioListGetBio(stringbioID, bool All);
}
}

3.Now need to add WCF Data Service in the web project. Let us add a service with name “BioInfo.svc”which will implement the interface IBioInfo.cs

Class

//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
using CommonLibrary;
using System.ServiceModel.Activation;

namespace SitecorePOC
{
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BioInfo : IBioInfo
{
public BioList GetBio(string bioID, bool All)
{
BioList bioList = new BioList();
return bioList.GetAllBios(bioID, All);
}
}
}

4. GetBio method internally call a function GetAllBios of CommonLibrary where actual logic is implemented to get the data based on parameters

using Sitecore.Data.Items;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace CommonLibrary
{
[DataContract]
public class BioList
{
public BioList()
{
}
private List iPeople = new List();
[DataMember(Order = 1)]
public List Persons { get { return iPeople; } set { iPeople = value; } }

public BioList GetAllBios(string bioID, bool All)
{
Sitecore.Context.SetActiveSite("website");

BioList BioList = new BioList();
List item = new List();

//Getting a specific bio feed based on its GUID
if(!string.IsNullOrEmpty(bioID))
{
var bioItem = Sitecore.Context.Database.GetItem(bioID);
if (bioItem != null)
{
Bios bio = new Bios()
{
Title = bioItem.Fields["Title"].Value,
Email = bioItem.Fields["Email"].Value,
GUID = bioItem.ID.ToString(),
FirstName = bioItem.Fields["FirstName"].Value,
LastName = bioItem.Fields["LastName"].Value
};
item.Add(bio);
}

}

//Getting all bio feed irrespective of GUID
if (All)
{
var Bios = Sitecore.Context.Database.GetItem("/sitecore/content/Home/Bios").Children;
foreach (Item bioItem in Bios)
{
Bios bio = new Bios()
{
Title = bioItem.Fields["Title"].Value,
Email = bioItem.Fields["Email"].Value,
GUID = bioItem.ID.ToString(),
FirstName = bioItem.Fields["FirstName"].Value,
LastName = bioItem.Fields["LastName"].Value
};

item.Add(bio);
}
}

return new BioList
{
Persons = item.ToList()
};
}
}
}

5. You need to add a DataContract

using System.Runtime.Serialization;
namespace CommonLibrary
{
[DataContract]
public class Bios
{
[DataMember(Order = 0)]
public string GUID { get; set; }
[DataMember(Order = 1)]
public string Title { get; set; }
[DataMember(Order = 2)]
public stringFirstName { get; set; }
[DataMember(Order = 3)]
public stringLastName { get; set; }
[DataMember(Order = 4)]
public string Email { get; set; }
}
}

6. In web.config file just add the behavior and service

<system.serviceModel>
<bindings>
<basicHttpBinding>
<bindingname="SitecoreApplicationCenter"closeTimeout="00:01:00"openTimeout="00:01:00"receiveTimeout="00:20:00"sendTimeout="00:05:00"allowCookies="false"bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"maxBufferSize="65536"maxBufferPoolSize="524288"maxReceivedMessageSize="65536"messageEncoding="Text"textEncoding="utf-8"transferMode="Buffered"useDefaultWebProxy="true">
<readerQuotasmaxDepth="32"maxStringContentLength="8192"maxArrayLength="16384"maxBytesPerRead="4096"maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behaviorname="web">
<serviceMetadatahttpGetEnabled="true" />
<serviceDebugincludeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<servicename="SitecorePOC.BioInfo"behaviorConfiguration="web">
<endpointaddress=""binding="webHttpBinding"contract="SitecorePOC.IBioInfo"></endpoint>
</service>
</services>
<serviceHostingEnvironmentaspNetCompatibilityEnabled="true" />
</system.serviceModel>

7. After building the project when you will hit the urls you will able to get output as XML which can be consumed anywhere.
i. http://sitecorepoc/bioinfo.svc/?All=true

XML

ii. http://sitecorepoc/bioinfo.svc/?ID={DD87A2B9-0D43-4EB7-A54B-8B113B27F06A}

XMLId

To Get JSON output you need to change WebMessageFormat from Xml to Json

Json

http://sitecorepoc/bioinfo.svc/?ID=DD87A2B9-0D43-4EB7-A54B-8B113B27F06A

JsonFeed

Advertisements