SharePoint: Getting a SPField with no exceptions to be thrown
As you probably know, a SharePoint field has a few names: display name, internal and static. The display name usually differs from the internal and static ones. In some exotic cases, the internal and static names differ from each other too. List’s fields can be reached through the SPList.Fields collection of the SPFieldCollection class.
Get SPField by different names in SharePoint 2007
In SharePoint 2007 the SPFieldCollection exposes a few methods to get a field by its known display or internal name, but not the static name. These methods are the indexer of the SPFieldCollection that accepts the field’s display name, the GetFieldByInternalName method accepting the internal name, and the GetField method accepting both display and internal names. Unfortunately, all these methods are case sensitive and throw an exception if the field with the passed display or internal name wasn’t found. I don’t like to wrap every piece of code into try-catch, so, for SharePoint 2007 applications I’m involved in, when it’s possible I use the simple method shown below:
public static SPField GetFieldByName(SPList spList, string displayNameOrInternalNameOrStaticName) { displayNameOrInternalNameOrStaticName = displayNameOrInternalNameOrStaticName.ToLower(); foreach (SPField spField in spList.Fields) { if (spField.Title.ToLower() == displayNameOrInternalNameOrStaticName) return spField; if (spField.InternalName.ToLower() == displayNameOrInternalNameOrStaticName) return spField; if (spField.StaticName.ToLower() == displayNameOrInternalNameOrStaticName) return spField; } return null; } // how to use // ... using (SPSite spSite = new SPSite("some site url")) using (SPWeb spWeb = spSite.OpenWeb()) { SPList spList = GetListByUrl(spWeb, "Lists/Products"); SPField spField = GetFieldByName(spList, "product name"); // the field's real display name is Product Name // do something } // ...
*Note: find the GetListByUrl method in the previous blog post – SharePoint: Getting SPList with no exceptions to be thrown
The GetFieldByName accepts a field’s all possible names including the static name, it’s not case sensitive and returns null if the sought-for field doesn’t exist.
Of course, I’m aware that enumerating fields takes more time than retrieving them from the SPFieldCollection’s internal hashtables so as the built-in methods do. But when time isn’t so crucial for a particular piece of code, I prefer using the GetFieldByName method. In addition I don’t have an alternative for the GetFieldByName when I know only the field’s static name.
Get SPField by different names in SharePoint 2010
In SharePoint 2010 the new TryGetFieldByStaticName method has been added to the SPFieldCollection class. So, as the method’s name implies, we get a field by its static name, and no one exception even will be thrown in case the field doesn’t exist. Thus, for SharePoint 2010 I’ve modified the GetFieldByName as follows:
public static SPField GetFieldByName(SPList spList, string displayNameOrInternalNameOrStaticName) { SPField spFieldByStaticName = spList.Fields.TryGetFieldByStaticName(displayNameOrInternalNameOrStaticName); if (spFieldByStaticName != null) return spFieldByStaticName; displayNameOrInternalNameOrStaticName = displayNameOrInternalNameOrStaticName.ToLower(); foreach (SPField spField in spList.Fields) { if (spField.Title.ToLower() == displayNameOrInternalNameOrStaticName) return spField; if (spField.InternalName.ToLower() == displayNameOrInternalNameOrStaticName) return spField; if (spField.StaticName.ToLower() == displayNameOrInternalNameOrStaticName) return spField; } return null; }
Check whether a field exists
To check whether a field exists, we can use the following method based on the GetFieldByName:
public static bool FieldExist(SPList spList, string displayNameOrInternalNameOrStaticName) { return GetFieldByName(spList, displayNameOrInternalNameOrStaticName) != null; }