Quick tip: Retrieving Sitecore Items from a WCF Service

In order to retrieve Sitecore Items from inside of a WCF Service you can get the database using var db = Sitecore.Configuration.Factory.GetDatabase("web"); and then simply call var item = db.GetItem("sitecore/content/Home/Item");.
However, the WCF service is completely unaware of the Sitecore Context so if you want the LinkManager to work as expected (instead of returning the full path to the item with /sitecore/content/Home/Item in the beginning of the URL), use SiteContext instead;

var siteContext = Sitecore.Configuration.Factory.GetSite("website");
siteContext.Database.GetItem("sitecore/content/Home/Item");

Then, overload the LinkManager.GetItemUrl(); with UrlOptions, specifying siteContext:

LinkManager.GetItemUrl(item, new UrlOptions { Site = siteContext; }

This will get you a clean URL (/Home/Item) with the Site’s context applied.

Posted in Sitecore Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*