Sitecore services Client(SSC) - SeviceAPIController Rest
Business has given the requirement to access the content of the items based on a template using some criteria by passing a parameter, this content will be used by the different opensource application and mobile device. So, I have decided to create an API using Sitecore service client(SSC)
Step 1:
Create the template for the item that needs to be searched.
Step2:
Create the class which inherits from ServiceAPIController (It is a WebAPI controller in asp.net but with all the security available for the items provided by Sitecore)
namespace Company.Feature.RestraurantsAPI.Controllers
{
[ServicesController]
public class RestaurantController : ServicesApiController
{
// GET: Restaurant
[HttpGet]
public IHttpActionResult Get(string id)
{
string CTAUrl = String.Empty;
List<Restaurant> restaurantlist = new List<Restaurant>();
using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
var searchQuery = context.GetQueryable<SearchResultItem>().Where(p => p.TemplateName=="Restaurants" && p["RestaurantCode"] == id).Select(i => (Item)i.GetItem()).ToList();
foreach (var item in searchQuery)
{
Item mediaItem = null;
string imageURL = string.Empty;
Sitecore.Data.Fields.ImageField imageField = item.Fields["RestaurantImage"];
if (imageField != null && imageField.MediaItem != null)
{
mediaItem = imageField.MediaItem;
var mediaOptions = new MediaUrlOptions { AbsolutePath = true, AlwaysIncludeServerUrl=true };
var mediaUrl = MediaManager.GetMediaUrl(imageField.MediaItem, mediaOptions);
imageURL = mediaUrl;
}
LinkField bookTableCTA = item.Fields["BookTableCTA"];
switch (bookTableCTA.LinkType.ToLower())
{
case "internal":
CTAUrl= bookTableCTA.TargetItem != null ? LinkManager.GetItemUrl(bookTableCTA.TargetItem) : string.Empty;
break;
case "external":
CTAUrl= bookTableCTA.Url;
break;
}
Restaurant restaurant = new Restaurant()
{
RestaurantCode = item["RestaurantCode"],
BookTableCTA = CTAUrl,
Contact = item["Contact"],
Description = item["Description"],
DressCode = item["DressCode"],
Email = item["Email"],
Location = item["Location"],
RestaurantImage = imageURL,
RestaurantName = item["RestaurantName"],
SpecialOffers = item["SpecialOffers"],
Timings = item["Timings"]
};
restaurantlist.Add(restaurant);
}
}
if (restaurantlist.Count() > 0)
{
return Ok(restaurantlist);
}
else
{
return BadRequest("No Restaurant found for this code: "+id) ;
}
}
}
}
Step 3:
Provide a valid route for the API as we not going to use the default route provided by Sitecore, by defining [ServicesController] Sitecore will automatically provide the default route. Let’s define the custom route.
namespace Company.Feature.RestraurantsAPI.Pipeline
{
public class RestaurantRoute
{
public void Process(PipelineArgs args)
{
RouteTable.Routes.MapHttpRoute("RestaurantApi", "Api/Restaurants/{id}", new
{
controller = "Restaurant",
action = "Get"
});
}
}
}
Step 4:
Finally, config update to patch the route pipeline we just created.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<initialize>
<processor patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']"
type="Company.Feature.RestraurantsAPI.Pipeline.RestaurantRoute, Company.Feature.RestraurantsAPI" />
</initialize>
</pipelines>
</sitecore>
</configuration>
Output:
Happy Programming :)
Comments
Post a Comment