Sitecore Wildcard Items

In a recent presentation to the DC Sitecore User Group, I was surprised to learn that most of the technical attendees didn’t know about Sitecore Wildcard Items. This hidden gem has been in Sitecore since at least Sitecore 4, and allows you to resolve item data however you like. For those of you with an ASP.NET MVC background, it’s like defining a route parameter at a particular URL segment. It’s an interesting example of what you can do with an httpRequestProcessor in Sitecore’s httpBeginRequestPipeline.

The original implementation was described by John West, former CTO of Sitecore, but his original blog post is lost to the Internet. You can find a discussion of Wildcard Items on page 39 of his book, Professional Sitecore Development.

Here’s how it works: Within the Sitecore content tree, you give an item a name of “*”. This item will act as a wildcard, matching any item at that level that doesn’t already have a sibling whose name explicitly matches that URL segment.

The beauty of this is that you can treat these items as regular Sitecore items — you can set presentation details, add them to workflow, etc. — but you can map the data to other Sitecore items or even an external database. For example, Sitecore uses wildcard items to resolve products within their Sitecore Commerce Reference Storefront implementation.

To implement a Wildcard Item, you’ll need to create two things:

  1. A Sitecore Wildcard Item Resolver
  2. An optional LinkProvider, so you can generate valid URLs for these items and reference them elsewhere in the site.

If you want to take a crack at implementing this yourself, have a look at Gaurav Agarwal’s post on Resolving the Wildcard Item, which has some code snippets to get you started.

There’s also an old Sitecore Wildcard Module, which does essentially the same thing, but uses the Sitecore Rules Engine to resolve the correct item from the Sitecore content tree. See Adam Conn’s post on Wildcards and Data-Driven URLs for details. It’s been available since 2011 with Sitecore 6, but could be modified to work in modern versions of Sitecore with a little work. I found a developer that created a revised wildcard module for Sitecore 7, for example.

Also, when using ASP.NET MVC, keep in mind that sometimes other pipelines might reset the Sitecore context item after your custom wildcard item resolver finishes its work. See Kamruz Jaman’s post on the Sitecore MVC Context Item for help troubleshooting this issue.

Wildcard items are a powerful technique, and can save a lot of hassles — I’ve seen many implementations “break out” of Sitecore to use MVC routing, then hack back in a Sitecore context or session object. But wildcard resolution keeps you within  within the Sitecore stack and are a more natural approach to this problem. You can learn a lot about how Sitecore’s request handling pipelines operate by studying how it works and applying it in your own solutions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s