Friday, September 28, 2012

Call GetSalePrice from EPiServer scheduled job

Parts of the EPiServer Commerce API requires that you have an active HttpContext. This will make life hard when you try to call this methods from a scheduled job, one of these methods that require an HttpContext is the "Mediachase.Commerce.Website.Helpers.StoreHelper.GetSalePrice", it checks for a value in the users cookie and if none is found it will take a default value, but if the context does not exist it will just throw an null pointer exception and fail. To fix this I just added the following code to the start of my scheduled job,

if (HttpContext.Current == null)
{
 HttpContext.Current = new HttpContext(new HttpRequest("index.aspx", "http://tempuri.org/index.aspx", string.Empty), new HttpResponse(null));
}