LibrarySites.Banner

Wildcards and Data Driven URLs

Wildcard items in Sitecore are a convenient way to handle dynamic URLs. They let you pass data through the URL instead of relying on query string values that are appended to the URL. Then it is up to you to figure out where that data is in the URL, which is nothing difficult, but if you have a lot of wildcards in your site and a lot of controls that depend on them, this can become a management headache.

The Wildcard Module available on the Sitecore Shared Source library is designed to make handling wildcard items easier. This post explains how.

Before I get into the Wildcard Module, I want to explain the problem the module is designed to address. As an example, I will use a site that displays a list of blogs. The blogs are managed by an external system, but I want to integrate that external data with my Sitecore site.

The list of blogs should appear at http://localhost/blogs.aspx. I have a control on this item that reads the external data and generates links for each blog. When a visitor clicks the blog link, a Sitecore page displays details about that specific blog.

The external system provides an API that exposes all of this information. In order to retrieve details about a specific blog, the API requires I identify the group name and the blog name. This means I have 2 parameters I need to pass. For SEO reasons, I want the links to look like the following:

  • http://localhost/blogs/travel/euroblog.aspx
  • http://localhost/blogs/nightlife/nycitylife.aspx

The Wildcard Module is designed to address 2 challenges presented by this example:

  1. How to generate the URLs that include all of the required parameters.
  2. How to read the parameters from the generated URLs.

The Wildcard Module allows you to define routes to wildcard items. A route is a set of rules that explain how to handle variables (wildcard items) that appear in an item's path, and which items the rules apply to.

In this example, the rules are:

  • Replace the first wildcard with the blog's group name
  • Replace the second wildcard with the blog's name

The Sitecore item that the route applies to is located at /sitecore/content/home/Wildcard Module Examples/Blogs/*/*.

The following screenshot shows what a route looks like in the Content Editor.

Wildcard Module route

 

Once this route is defined, I can use it to generate the dynamic URLs and to read the parameters from the URL. The following code shows how external data can be used to generate the dynamic URLs:

var path = "/sitecore/content/home/Wildcard Module Examples/Blogs/*/*";
var item = Sitecore.Context.Database.GetItem(path);
var ts = WildcardManager.Provider.GetWildcardUrl(item);
var data = new NameValueCollection();
data.Add("GROUP NAME", "travel");
data.Add("BLOG NAME", "euroblog");
var url = ts.ReplaceTokens(data);

 

The following code shows how values can be read from a dynamic URL:

var path = "/sitecore/content/home/Wildcard Module Examples/Blogs/*/*";
var item = Sitecore.Context.Database.GetItem(path);
var ts = WildcardManager.Provider.GetWildcardUrl(item);
var url = "/blogs/nightlife/nycitylife.aspx";
NameValueCollection tokens = ts.FindTokenValues(url);

 

Want to see more? Watch a demo video of the module in action.