A better way to obtain fields values of an SPListItem
.NET, Best Practices, Development, Productivity, SharePointObtaining the value of a particular field (SPField) of a list item (SPListItem) is quite awkward. First of all you have to pass either the index, Id or the InternalName of an existing field. If the field exists a value of the object type is being returned. In some cases that value can be null. If it’s not you can cast it to its origin type as in most cases defined by the SPField.FieldValueType Property. Imagine doing all that each time you want to retrieve a field value. Isn’t there a better way?
Introducing the TryGetValue extension method
To simplify the task as described above I have created an extension method using generics:
What is it doing?
The method does exactly the same what I describe above: it retrieves the value of the given field for the current list item. Because it might just happen that you’re passing an invalid id, the whole process of retrieving and processing the value is wrapped in a try catch clause.
The method encapsulates the whole process of retrieving and process the value what leads to simplification of your code. Compare the standard approach of retrieving a field value with how you would do the same using the extension method:
See the difference? Using the extension method all you need is one line of code. The value you get is type safe and you know is different than null.
Under the hood
I have chosen to make it an extension method to make using it more intuitive. Getting the value of the particular field of the particular list item is what you want to do: why not calling the method like that?
Furthermore I have decided to use generics to provide the type safety of the retrieved value. Like that you have just one method which works for every single type out there including your custom field types:
To provide you some feedback about the value I have decided to use the try pattern. The method tries to retrieve the value of the particular field. It returns true if succeeded and false if failed. To prevent you from making an extra call to get the value I have decided to use an out parameter so you can directly use the value in your custom code.
As you’ve probably noticed in the example above I’m referring to fields using classes instead of typing the internal names. Using the latest version of Imtech Fields Explorer you can automatically generate wrapper classes for content types so you will never have to use any internal name again.
The TryGetValue extension method: do you love it or hate it?














November 14th, 2008 at 3:07 pm
Excellent code. I am sure it will save a lot of people a lot of hassle, including me
One question/remark.
You say at one point "Because it might just happen that you’re passing an invalid id, the whole process of retrieving and processing the value is wrapped in a try catch clause."
Is there a specific reason why you would not use the following:
if (listItem.Fields.ContainsField("MyDateField"))
{
// Enter retrieval code here
}
This seems to me a typical situation in which you can save yourself (and the CLR) the trouble of using Exceptions. But I might be missing something.
Regards,
Erik
November 14th, 2008 at 4:23 pm
Thanks for the tip Erik, I've definitely missed that one. Adding the extra check you mentioned would keep you off from getting an extra Exception but I still like the idea of having it all wrapped in a try..catch clause 'just in case'.
November 24th, 2008 at 8:23 pm
The only problem in using the ContainsField method is it only accepts a string that contains either the display name or the internal name of the field. The wrapper classes use IDs.
By the way Love It
November 24th, 2008 at 8:33 pm
Thanks TJ
December 3rd, 2008 at 7:39 pm
Quick question on your sample caller code:
MyContentType.MyDateField
Do you have custom classes in your code that inherit from SPContentType to do this or is there something else at play?
December 3rd, 2008 at 9:11 pm
@Jim: It's a wrapper class generated using Imtech Fields Generator v1.5 (http://blog.mastykarz.nl/imtech-fields-explorer-v1410-inconvenient-site-columns-update/).