I need an assist. I'm running into a situation where code that was written for a custom rule is not firing. I have put breakpoints in Visual Studio and the breakpoints are never hit.
My solution is an Sitecore MVC website using Sitecore version 8.1 Update 3. The Rule is configured through Presentation Details of a rendering component.
The custom rule code was a WhenCondition after getting feed back from Mark Cassidy, it's been changed to a StringOperatorCondition.
I have done the following:
- Created a Rule Folder item and added it the the Rule item's Tags under Default
- Created Element Folder
- Created Rule Condition item
- In the Data section Text field there is this text:
where the cookie [operatorid,StringOperator,,compares to] [Value,,,specific value]
- In the Script section Type field I have put the
MyNamespace.Class, Assembly Name
- In the Data section Text field there is this text:
- Added Rule to Tags in the Default Tags Definition
- Created Rule Condition item
I am looking for debugging suggestions. On the surface this seems like I haven't configured something correctly. I can't see what the is. In the Rules Cookbook for 7.1+, there is mention that the rule should be added to the Standard Values of a template. Is this really needed in Sitecore 8.1 Update 3?
***UPDATE: I have double checked my configuration against Sitecore 8 – Create a custom personalization rule. I changed my code as @MarkCassidy suggested. (I am in no way saying what Mark suggested is any reason why the logic is not being called. What he suggests seems like the right approach.) I am still scratching my head as to what I'm missing. I also changed the Text in my Rules Condition. The value is updated above.
**SOLUTION: The solution ultimately was the I had the wrong xdb setting enabled. I had xdb.enabled, and xdb.tracking.enabled = false, so the rules weren't firing. Mark pointed that out in the solution comments below.
Here is the refactored custom rule code (After this refactor the rule is still not firing):
public class ZipCookieCondition<T> : StringOperatorCondition<T> where T : RuleContext
{
public string Value { get; set; } //StateName
protected override bool Execute(T ruleContext)
{
Assert.ArgumentNotNull(ruleContext, "ruleContext");
var locationCookie = HttpContext.Current.Request.Cookies["LocationCookie"];
if (locationCookie == null)
{
return false;
}
//Cookie is Http Encoded because front-end uses Cookie.js and is
//storing a JSON object as the value
var cookieVal = string.IsNullOrEmpty(locationCookie.Values["state"])
? DecodeCookieValue(locationCookie)
: locationCookie.Values["state"];
if(!string.IsNullOrEmpty(cookieVal))
return Compare(cookieVal.ToLowerInvariant(), Value.ToLowerInvariant());
return false;
}
private string DecodeCookieValue(HttpCookie locationCookie)
{
var cookieValue = HttpUtility.UrlDecode(locationCookie.Value);
var locationObj = JsonConvert.DeserializeObject<LocationCookie>(cookieValue);
if (locationObj != null)
{
return locationObj.State;
}
return string.Empty;
}
}
internal class LocationCookie
{
public string State { get; set; }
public string ZipCode { get; set; }
}
normal
mode