Global Office Directory
More Products
Share this page
Home > Community > Technical Blogs > Getting to Know Sitecore > 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:
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.
Tags: API, Information Architecture
- Younes Ruth September 21, 2011 at 7:23 AM
Adam is a technical architect on Sitecore's Product Marketing Team. He is responsible for spreading technical knowledge inside and outside of the company, with an emphasis on external systems and applications.
This website is designed to be fully functional with scripts disabled in browser. Please contact the webmaster for any suggestions