This is the second post in the Sitecore and FxCop series. Read the first post here.
The second rule I came up with has to do with how to access raw field values.
Let’s say we want to grab the raw value of the Title field on the current item. There’s two different options we can use:
var fieldValue = Sitecore.Context.Item.Fields["Title"].Value;
Which really isn’t correct – what if there is no Title field? Or what if we have a typo in the fieldname? We’d end up with a NullReferenceException. To get around this we could use:
var field = Sitecore.Context.Item.Fields["Title"]; if (field != null) { var fieldValue = field.Value; }
The other option is:
var fieldValue = Sitecore.Context.Item["Title"];
In this case, if there is no Title field, or if we have a typo in the fieldname, we’d get an empty string.
To be absolutely clear, this is only to get the raw field value. To display it on the front end side of things usually one would use a FieldRenderer instead anyway.
So, the second option is shorter and safer. Seems like a good thing to check for.
In the Rules.xml we need to add the following to our existing <Rules> definitions:
<Rule TypeName="AccessRawFieldValueProperly" Category="Sitecore.BestPractice" CheckId="SC002"> <Name>Get the raw field value properly</Name> <Description> Accessing the raw field value through the Field property on the item can throw NullReferenceExceptions when the fieldname changes, the field doesn't exist or there's a misspelling in the fieldname. </Description> <Url /> <Resolution> Raw field value is accessed by Item.Fields["Fieldname"].Value. Raw field values should be accessed by Item["fieldname"] </Resolution> <Email /> <MessageLevel Certainty="70">Error</MessageLevel> <FixCategories>Breaking</FixCategories> <Owner /> </Rule>
As always, the TypeName needs to match the name of the class.
internal sealed class AccessRawFieldValueProperly : BaseRule { public AccessRawFieldValueProperly() : base("AccesssRawFieldValueProperly") { } public override ProblemCollection Check(Member member) { var method = member as Method; if (method != null) { VisitStatements(method.Body.Statements); } return this.Problems; } public override void VisitMethodCall(MethodCall methodCall) { var memberBinding = methodCall.Callee as MemberBinding; if (memberBinding != null) { var methodCalled = memberBinding.BoundMember as Method; if (methodCalled != null) { if (methodCalled.FullName == "Sitecore.Data.Fields.Field.get_Value") { Problems.Add(new Problem(GetResolution(), methodCall)); } } } base.VisitMethodCall(methodCall); } }
Then, when I have a solution with the following code:
public void SomeMethod() { var rawFieldValue1 = Sitecore.Context.Item.Fields["fieldname"].Value; var rawFieldValue2 = Sitecore.Context.Item["fieldname"]; var field = Sitecore.Context.Fields["fieldname"]; }
I would expect to get one error in FxCop. I still want to be able to grab the actual field of course.
When I have a couple more rules in here I’m thinking about releasing this to the Sitecore Marketplace, so your input is very welcome. Is there any rule you would like to see added?