Inconvenient Catalog Item Page and ‘Page not found’ (404) experience


Using one Catalog Item Page you can publish the contents of hundreds of items on your website. But what will you see if you type the wrong URL?

SharePoint 2013 Search-driven publishing

One of the great improvements in SharePoint 2013 is the search-driven publishing model, where the content is published using SharePoint 2013 Search. A particularly interesting scenario of leveraging the search-driven publishing model is cross-site publishing, where content is stored in one or more catalogs and published on your public website.

When working with cross-site publishing, the content is retrieved from SharePoint 2013 Search index and rendered using a Catalog Item Page. A Catalog Item Page is a Publishing Page with a number of Catalog Item Reuse Web Parts that are responsible for retrieving the right item and rendering its contents on the page. Following screenshot present a sample blog post published using the SharePoint 2013 cross-site publishing capability on the mavention.com website:

Blog post published using the SharePoint 2013 cross-site publishing capability on the mavention.com website

In most scenarios one of the Catalog Item Reuse Web  Parts placed on the Catalog Item Page is responsible for retrieving the right catalog item based on the information from the URL. And while it works just fine when the URL is typed as expected, the problems start when the URL refers to a catalog item which does not exist.

Catalog Item Not Found

If your visitors mistype a URL of a catalog item, all that they will see is an empty page:

Empty page displayed after mistyping the URL of a catalog item

Although SharePoint 2013 allows you to configure a ‘Page not found’ page, it isn’t displayed for non-existing catalog items.

So why is it happening and, what’s more important, what can we do about it?

How it works: catalog items

When publishing content from catalogs there are two types of pages that you have to configure: the Category Page and the Catalog Item Page. Both are routed to, using the mapping information stored in the navigation taxonomy of your Site Collection. Following screenshot presents a sample configuration of the Category and Catalog Item pages for a navigation term.

Category and Catalog Item pages configuration for a navigation term

When you configure a Catalog Item Page for a navigation term, every URL that is located below that term will be rerouted to that Catalog Item Page, for example if Blog is our category located at the /blog URL, then the blog post about us launching the mavention.com website, located at /blog/mavention-com-live URL will be rerouted to the Catalog Item Page responsible for rendering blog posts.

Unfortunately during the rerouting process SharePoint doesn’t check if the requested catalog item exists or not: after all the logic that you configured for retrieving the catalog item is stored on the Catalog Item Page to which SharePoint is rerouting you and there is no way SharePoint could guess it.

Once on the Catalog Item Page, the Catalog Item Reuse Web Part located there takes over the control. Using the information from the URL it executes a search query which returns the requested catalog item which is then rendered on the page.

The challenge with the Catalog Item Reuse Web Part is that it’s no more than a Search Web Part: it either finds something or not; it contains no additional logic based on the number of retrieved results. As a consequence, if no catalog item is found, no information is rendered on the page.

Better Catalog Item Not Found experience

From the end user perspective there should be no distinction between how the website handles non-existing Publishing Pages, Category Pages and Catalog Item Pages. In either of those cases the website should let the visitor know that the requested page does not exist and it could suggest some alternatives based on the requested non-existing URL. To put it simple, it should display a proper ‘Page not found’ page.

In order to make the Catalog Item Reuse Web Part distinct between existing and non-existing catalog item URLs you have to extend it and check the number of retrieved results. The following snippet presents the extended Catalog Item Reuse Web Part that handles non-existing catalog item URLs:

public class MaventionCatalogItemReuseWebPart : CatalogItemReuseWebPart {
    protected override void Render(System.Web.UI.HtmlTextWriter writer) {
        if (GetNumResults() < 1) {
            HttpContext.Current.Server.TransferRequest(String.Format("{0}?url={1}", SPContext.Current.Site.FileNotFoundUrl, HttpContext.Current.Request.RawUrl));
        }
        else {
            base.Render(writer);
        }
    }

    private int GetNumResults() {
        int numResults = -1;

        ResultTableCollection syncResult = AppManager.GetSyncResult(QueryGroupName);
        ResultTable rt = syncResult.Filter("TableType", TargetResultTable).FirstOrDefault();
        if (rt != null) {
            numResults = rt.TotalRows;
        }

        return numResults;
    }
}

If you would use this extended version of the Catalog Item Reuse Web Part instead of the one provided with SharePoint 2013 and would navigate to a URL of a non-existing catalog item, instead of an empty page you would be presented with a ‘Page not found’ page:

The ‘Page not found’ page displayed when trying to navigate to a non-existing catalog item

Summary

One of the new capabilities of SharePoint 2013 is cross-site publishing where content stored in a catalog can be published on one or more Site Collections. Unfortunately, when using cross-site publishing, SharePoint doesn’t handle properly non-existing catalog item URLs by itself. By leveraging the extensibility capabilities of SharePoint 2013 you can add support for proper handling of non-existing catalog item URLs with minimal effort.

Others found also helpful: