Friday, September 6, 2013

Sharepoint Field Names With Spaces

If the SharePoint column's name contains spaces, we have to do some tricks to work with that column from code. There are two types of field names: internal name - a static one, and display name, which can be changed by users every time they want.

So, you should have already guessed that the static one, i.e. the internal name of the field must be used from codebehind and even from client object model (COM). The internal name is set once the field is created, based on its initial name, and it won't be changed any more. This assumes, that you must be careful naming the field at create time. It is convenient to use only latin characters without any spaces at first, and after saving the field, change it the way we want.
Why shall we do so?
Try not to follow these simple rules, and create a list column named "Assigned To" (no matter what type is it). After saving the new column, go to the list settings and click on the newly created "Assigned To" column, which will lead you to the edit page. Pay attention to the URL. At the end of it you will see Field=Assigned%5Fx0020%5FTo
This means that the internal name of the column is Assigned%5Fx0020%5FTo. But if you try to use it in your code, you may be faced to a NullReferenceException, which simply means "There is no such column named Assigned%5Fx0020%5FTo". So you have to understand what the additional characters are in the column name to avoid such a headache!
Lets start with the space in the name "Assigned To", it is converted to _x0020_ at first, but the underscore characters are being replaced by %5F, so if you want to solve this problem, you must either delete and recreate your column with a simple name, or you must use Assigned_x0020_To to be able to use the field from code!

No comments:

Post a Comment