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:
The Wildcard Module is designed to address 2 challenges presented by this example:
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:
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.
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"
);
"BLOG NAME"
"euroblog"
var url = ts.ReplaceTokens(data);
The following code shows how values can be read from a dynamic URL:
var url =
"/blogs/nightlife/nycitylife.aspx"
NameValueCollection tokens = ts.FindTokenValues(url);
Want to see more? Watch a demo video of the module in action.
Thanks for the video!
Do you know which blogging platforms allow you to programmatically access blog data like this?
Sitecore Community Solution Accelerator and Telligent use this approach.
How can we use Sitecore Wildcard with MVC3?
An example of this can be found on Sitecore Commerce Powered by Commerce Server example site called "Reference Store" at github: github.com/.../Reference-Storefront
Are there any extra steps to get wildcards working for cultures? For example I set up a route and it works fine with base English(en) but when I switch the culture to US English(en-US) the tokens are not being found.
After pounding away I found the answer to my question above regarding cultures. The tokens I created were not being recognized when the Url had an uppercase culture for example en-US. When I set the Url ToLower() the tokens are recognized. Old code: var myToken = GetTokenValue(Sitecore.Context.Item, Sitecore.Context.Site, token, url) ?? ""; New code: var myToken = GetTokenValue(Sitecore.Context.Item, Sitecore.Context.Site, token, url.ToLower()) ?? ""; Thanks Adam!
A bit late but really good post :)