Thursday, March 4, 2010

We must use internal names.

Recently I was testing the application we designed and developed in SharePoint 2007, on SharePoint 2010, the main and least expected  problem that I discovered was the usage of display names to retrieve the values of the some fields. During the development, of course we had a common understanding to use the internal name whenever we deal with the fields as that is the unique  for the SPField but we have slipped few places and never  used the internal names, these retrieves were happened to be in the workflow module where we retrieve the field values from the workflow task list items, to edit  the respective tasks. Fortunately, the display names of some fields of the task list are changed from 2007 to 2010, I said, fortunately, because, if it is not for this, the bug will stay dormant in our production code for bit longer time.

Even this is a well know fact, I decided to share this finding with the development community to re-emphasize the usage of internal names and also to highlight the changes  in task list.

The problem I encountered is, in the task list for work flows, some of the display names are differrent in 2010. For example, we had a "Link" field in 2007 and the same field is renamed to "Related Content" in 2010, but the good thing is, the internal name remain the same  "WorkflowLink".

For the help of the beginners, this probably the way to go.

private object GetFieldValueByInternalName(string internalName)
{
              object rawValue = null;
              SPField fieldToExamine = null;

              if (null != TaskListItem)
             {
                   //Get the field by the internal name
                   fieldToExamine = TaskListItem.Fields.GetFieldByInternalName(internalName);
                 
                  if (null != fieldToExamine)
                 {
                         //Field does exist, get the value
                         rawValue = TaskListItem[fieldToExamine.Id];
                  }
             }
             return rawValue;
}


No comments: