WebAPI 2 native support in Sitecore 8.2

webapi-pic-300x300-2-2

Here we go with an other episode of the WebAPI and┬áSitecore saga…

As you probably know each version of Sitecore has managed WebAPI in a different way….

https://sitecorecommerce.wordpress.com/2014/11/30/webapi-attribute-routing-is-not-working-with-sitecore-7-5/

http://kamsar.net/index.php/2014/05/using-web-api-2-attribute-routing-with-sitecore/

Within 8.2 finally Sitecore is officially supporting WebApi NATIVELY

So what do you need to do to write your first WebAPI Controller and start using it?

very simple:

public class MyCustomController : Controller
{
   public ActionResult Test()
   {
      return Content("This is a test");
    }
}

and this is the Url where you can access your controller

Url: http://yourInstanceName/api/sitecore/MyCustom/test

public class MyOtherCustomController : Controller
{
   public ActionResult OtherTest(string mycustomId)
   {
      return Content("This is a test with params" + mycustomId);
    }
}

Url: http://yourInstanceName/api/sitecore/MyOtherCustom/OtherTest/thisIsMyParam

will return “This is a test with params thisIsMyParam”

Happy Sitecore and happy webApi to everybody!

Custom RSS Atom Feed in a Sitecore Application

I had the necessity to customize the order and the number of items provided in the RSS feed generated within Sitecore and I have written the following code…

I am not particularly proud of it, but there are scenarios where this is the easiest solution to fix a production issue….

You could also reuse the following code to expose a custom RSS endpoint within your Sitecore web site

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Sitecore.Data.Items;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Net.Http;
using System.IO;
using System.Net; 

public class CustomRSSController : ApiController
  {
     [HttpGet]
public HttpResponseMessage GetNews()
     {
     string url = "http://yourWebSite.com/rss" ;
      XmlReader reader = XmlReader.Create(url);
      SyndicationFeed feed = SyndicationFeed.Load(reader);
      reader.Close();

      var output = new MemoryStream();
      string xml;

      feed.Items = feed.Items.OrderByDescending(x=>x.PublishDate).Take(5);

      var formatter = new Rss20FeedFormatter(feed);
      var xws = new XmlWriterSettings { Encoding = Encoding.UTF8 };
            using (var xmlWriter = XmlWriter.Create(output, xws))
            {
                formatter.WriteTo(xmlWriter);
                xmlWriter.Flush();
            }
            using (var sr = new StreamReader(output))
            {
                output.Position = 0;
                xml = sr.ReadToEnd();
                sr.Close();
            }

 var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(xml, Encoding .UTF8, "application/atom+xml") };
            return response;        
       }