Wildcard Module by Template

Problem:

I wanted to use the Wildcard Module to generate dynamic URL’s of items located in a data folder for all the items in my content tree of a specific template. The problem is that the Wildcard Module only lets you associate items and not a template.

Fix:

After installing the Wildcard Module, I added a TreelistEx field to the /sitecore/templates/Wildcards/Route template called “Templates”.

Sitecore Wildcard Route Template

Sitecore Wildcard Route Template Change

After that I went to the /App_Config/Include/Sitecore.Marketing.Wildcards.config and replaced provider with:


<providers>
<clear />
<add name="sitecore" type="Site.Wildcards.WildcardProvider, Site.Web" />
</providers>

I then created a class that inherits from Sitecore.Marketing.Wildcards.WildcardProvider to override the RunRules method. The only line I changed was to add a check for the template in the Sitecore query.


using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Marketing.Wildcards.Rules;
using Sitecore.Rules;
using Sitecore.Data.Fields;

namespace Site.Wildcards
{
public class WildcardProvider : Sitecore.Marketing.Wildcards.WildcardProvider
{
protected override Sitecore.Marketing.Wildcards.Rules.WildcardRuleContext RunRules(Sitecore.Data.Items.Item item, Sitecore.Sites.SiteContext site)
{
Assert.ArgumentNotNull((object)item, “item”);
WildcardRuleContext ruleContext = new WildcardRuleContext(item, site);
Item i = Context.Database.SelectSingleItem(string.Format(“/sitecore/system/Modules/Wildcards/Routes/*[contains(@Items, ‘{0}’) or contains(@Templates, ‘{1}’)]”, item.ID.ToString() , item.TemplateID.ToString()));
if (i == null)
{
return ruleContext;
}
Field field = i.Fields[“Rules”];
if (field == null)
{
return ruleContext;
}
RuleList rules = RuleFactory.GetRules(field);
if (rules == null || rules.Count == 0)
{
return (WildcardRuleContext)null;
}
rules.Run(ruleContext);
return ruleContext;
}
}
}

Now I am able to set a template for a rule and all items in the content tree of that template will act as a wild card.

Door Route with Template

Door Route with Template

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: