Some days back “We need to populate an asp DropDownList control with all possible values of a choice column created in a specific SharePoint list“. All done but when we tried to populate dropdowns using oList.Fields, following exception generated:
Field with name “XYZ” was not found.
We were using the following code:
SPField oField = oList.Fields[“XYZ”];
We were using internal name to get the reference of SPField and it was generating exception. The reason is that oList.Fields take following three values:
|oList.Field[Guid id]||Gets the field using unique identifier|
|oList.Field[int iIndex]||Gets the field object at the specified index in the collection|
|oList.Field[string displayName]||Gets the field with the specified display name for the collection|
The trick is in the last one. We are used of getting list item value using internal name but here we are required to use display name. So, we switched our implementation to GUIDs as fortunately we are also creating list and fields using feature in the same project.
This also clears another concern that why we are required to use internal names instead of display name when accessing a list item specific field value using internal name like listItem[“Field Internal Name”]. It may be due to the fact that user can any time change the display name of the field and our code will crash. As in the above example we preferred to switch to GUIDs instead to display name. Other than this, it may also be due to that fact that there are some characters which needs to be changed when translating display name to a URL.