Normally when you create a Sitecore business rule you would define an operator configuration and let the user define the type of operator they want to use. An example of this is the "StringConditionOpertator" (found here /sitecore/system/Settings/Rules/Definitions/String Operators). In a lot of cases, the user would select what type of operation to perform on the value they set. By type I mean things like "equals" or "contains" etc. Below is a list of operators you can use for a string condition.
What if you wanted to create a custom rule that did a string operation but you always wanted it to be the same type of operator? You did not want to, for whatever reason, allow the user to specify how the string should be compared. You can do this by setting the OperatorId to the GUID from the item above. A word a warning here though. Make sure the string stays in the proper casing. If you create a constant for this and/or a GUID and do something like "ToString" the result could cause the casing to change. This is an issue because if you look at the "ConditionUtility" class you will notice all the methods in here setup enums based on a switch, and this switch expects an exact string match.
As you can see in the above code if for some reason your string casing was not all upper case you will not get a match on the operator. This will cause the "Compare" method to return false when you are expecting a true. This is because the operator ends up getting set to "Unknown".
Even though the Guid is correct it is not in the proper casing. It would be great if Sitecore updated this utility to be case insensitive since really they are after a GUID match and the casing does not matter. A simple "ToUpper" call on the switch would avoid people getting tripped up on this issue.
What if you wanted to create a custom rule that did a string operation but you always wanted it to be the same type of operator? You did not want to, for whatever reason, allow the user to specify how the string should be compared. You can do this by setting the OperatorId to the GUID from the item above. A word a warning here though. Make sure the string stays in the proper casing. If you create a constant for this and/or a GUID and do something like "ToString" the result could cause the casing to change. This is an issue because if you look at the "ConditionUtility" class you will notice all the methods in here setup enums based on a switch, and this switch expects an exact string match.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public static StringConditionOperator GetStringConditionOperatorById(string conditionOperatorId) { if (string.IsNullOrEmpty(conditionOperatorId)) return StringConditionOperator.Unknown; switch (conditionOperatorId) { case "{10537C58-1684-4CAB-B4C0-40C10907CE31}": return StringConditionOperator.Equals; case "{22E1F05F-A17A-4D0C-B376-6F7661500F03}": return StringConditionOperator.EndsWith; case "{2E67477C-440C-4BCA-A358-3D29AED89F47}": return StringConditionOperator.Contains; case "{537244C2-3A3F-4B81-A6ED-02AF494C0563}": return StringConditionOperator.CaseInsensitivelyEquals; case "{6A7294DF-ECAE-4D5F-A8D2-C69CB1161C09}": return StringConditionOperator.NotCaseInsensitivelyEquals; case "{A6AC5A6B-F409-48B0-ACE7-C3E8C5EC6406}": return StringConditionOperator.NotEqual; case "{F8641C26-EE27-483C-9FEA-35529ECC8541}": return StringConditionOperator.MatchesRegularExpression; case "{FDD7C6B1-622A-4362-9CFF-DDE9866C68EA}": return StringConditionOperator.StartsWith; default: return StringConditionOperator.Unknown; } } |
As you can see in the above code if for some reason your string casing was not all upper case you will not get a match on the operator. This will cause the "Compare" method to return false when you are expecting a true. This is because the operator ends up getting set to "Unknown".
// Setting the value to this means the operator will always be unknown. this.OperatorId = "{10537c58-1684-4cab-b4c0-40c10907ce31}" // This is all upper case so it finds the correct match. this.OperatorId = "{10537C58-1684-4CAB-B4C0-40C10907CE31}"
Even though the Guid is correct it is not in the proper casing. It would be great if Sitecore updated this utility to be case insensitive since really they are after a GUID match and the casing does not matter. A simple "ToUpper" call on the switch would avoid people getting tripped up on this issue.
Comments