Rien de spécial
Le blog de Régis

Best practice for changing the value of a column in SharePoint

In SharePoint development, it is pretty common to change the value of an element.

For instance, let’s consider the column named « Title » defined in many lists. Suppose we want to change the « Title » of an item. What’s sound straightforward is to write this

SPListItem item // get the needed item
item["Title"] = "New value";

Now to deply this solution on a French platform, and c’est le drame: Value does not fall withinh the expected range. That’s because the column is referenced here by its display name. So, for the French platform it should be

item["Titre"] = "New value";

Even worse. The display name is editable by end users (or more precisely by site administrators). Everyone get the point: this is code snippet is a worse practice.

The best practice is to use the internal name (which is « Title » for any language and will never change)

item[item.Fields.GetFieldByInternalName("Title").Id] = "New value";

And now, I can confidently say that SharePoint happy is developper unfriendly.

Note also, this sugguests another best practice: when you create a custom column: don’t use spaces in the internal name.