I recenty ran into some unexpected behavior from Sitecore when working with custom properties on user profiles. I were trying to get the value of a custom property on a user profile, but the returned value ended up being
null even though I was sure the property was there and that it had a value.
// This is null even though the user profile has a custom property named "Country" var country = Sitecore.Context.User.Profile.GetCustomProperty("country"); // Another way to get property values (not just custom properties) // but the result is the same as above var country = Sitecore.Context.User.Profile["country"];
It turns out that the property names are case sensitive – and the custom property on the user profile was
Country with a capital ‘C’!
Most of your are probably used to Sitecore not caring about casing when accessing item fields etc. at least I am.
// Both of these will return the raw value of the "Title" field var title1 = item["title"]; var title2 = item["Title"]; // Both of these will return the "Title" field var field1 = item.Fields["title"]; var field2 = item.Fields["Title"];
But that is not how the
GetCustomProperty method works and the reason is how the custom properties are stored internally in the
UserProfile class. The class uses a
Dictionary<string, string> to store the custom properties and the key is then of course case sensitive.
So beware when accessing custom properties on user profiles and make sure you are using the same casing as the field name.
Don’t know how to add custom properties to user profiles? Have a look at this blog post: ipointsystems.com/blog/2013/may/adding-cust..