Monday, 4 March 2013

Get the internal column name in Sharepoint 2010 from the display name

The internal column is easily obtainable from the Sharepoint object model.

A recent function required me to use the internal name instead of the (provided) displayname. The following function resolved the problem quickly and easily:

private string InternalName(string displayName)
 SPFieldCollection fields = SPContext.Current.Web.Lists["MyListName"].Fields;
 if (fields.ContainsField(displayName))
   if (string.Compare(displayName, "Title", true) == 0)
     return "Title";
     return fields[displayName].InternalName;
 throw new Exception("Column not found");

This function is not great, but it has solved my problem. The one caveat is the extracting of the 'Title' column. The function was returning 'LinkTitle', which caused the bulk update the fail quietly.

The hack fixed the problem. This is not my most elegant piece of code, but it does the business.

No comments:

Post a comment