Monday, April 11, 2016

Custom item resolver in Sitecore

I had a website migration to Sitecore with a large subset of URLs which could not be mapped using any of the available URL redirect modules (due to custom business logic). Therefore a custom item resolver had to be written.
public class MyCustomResolver : HttpRequestProcessor
    public override void Process(HttpRequestArgs args)
        // Return if Sitecore has found the item
        if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0) return;

        // If item not found
        var itemPath = args.Url.ItemPath; // The requested item path
        var context = Factory.GetDatabase("web");
        var item = context.GetItem("/"); // Business logic here to find the new item
        if (item != null)
            Context.Item = item; // If the item is found return it
<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="MyProject.MyCustomResolver , MyProject" />
This then gets referenced in the Web.config or Sitecore.config file after the default item resolver. That means that if Sitecore doesn't find the item it goes into this custom resolver. If the business logic doesn't find the correct item, it continues on through to a normal 404 error.

No comments:

Post a Comment