Basically I had to create a new class called ContextHelper:-
public static class ContextHelper
{
public static void EnsureUmbracoContext()
{
if (UmbracoContext.Current == null)
{
var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter())));
UmbracoContext.EnsureContext(
dummyHttpContext,
ApplicationContext.Current,
new WebSecurity(dummyHttpContext, ApplicationContext.Current),
UmbracoConfig.For.UmbracoSettings(),
UrlProviderResolver.Current.Providers,
false);
}
}
}
Then in my IAsyncResult file, I just called:-
IContentType contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("KnowledgebaseArticle");
var searchResults = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(contentType.Id);
ContextHelper.EnsureUmbracoContext(); // THIS IS NEW
UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
var searchResultsFiltered = searchResults.Where(x => x.Name.ToLower().Contains(searchBy.ToLower())).Select(x => new { x.Name, Path = helper.NiceUrl(x.Id) });
EnsureContext issue
Hello,
I'm trying to use the following code in a IAsyncResult:-
However, the results always comes back as:-
Value cannot be null. Parameter name: httpContext
I'm not that familiar with EnsureContext, so not sure I'm using it right. Can someone clarify how this should be done please.
Many thanks
Paul
I've managed to work out how to implement this.
Basically I had to create a new class called ContextHelper:-
Then in my IAsyncResult file, I just called:-
For more information take a look at this link:-
https://gist.github.com/sniffdk/7600822
Cheers
Paul
is working on a reply...