SPField (column) Field Name, Title and internal Names and Spaces between them

 I have been working with Share Point and MOSS for sometime as a developer and hate the confusion  with Title, Internal Name  for SPField (Column) for a list and development effort going being it.  I personally, if start building a site from scratch make sure there are absoulutely no spaces for any of my fields list name.  I later change the field names editing them according to given requirement.  Any developer who has worked with CAML queries and API's accessing list knows what  I am talking about.

Column Name is the name with which you create the column (field ) and no matter how many times you change it, It remains the same. 

 
This especially occurs when there is some kind of custom development within Share point (Custom list definition  templates, Web parts or Custom Field types). Usually all your development work is catered or intended to build something, which will meet all the needs and in fact be generic to quite an extent and thats where customizable web parts using Tool parts or Customizable Custom field type come in to play.

Reason I am mentioning Configuration is because thats the point where you interact with Users configuring and your code deciphers everything and  Boom! it works. Here are the different scenarios I am talking about.

I am creating a column called "Company Name" 

After Renaming the column

CAML Query Tool

Suppose you are working on a Web Part where you want users to Pick a list and few Columns and you do some custom logic and  render the Data.

Column NAME  :

1> Users say Column Name it means SPField.Title ( title could be  "ABC Company Name "  but Column name would be "CompanyName")

2> In you code if you are using  Splist.Fields[" Field Name"] or  listItem["Field Name"]  you would be using the Internal Column Name

 E.G:
Title = "Company Name"   ColumnName(internal name) =  "Comp Name"     You would use  listItem["Comp Name"]

3> Caml Queries:

You are using some logic and you don't need to traverse through the entire list items and  you decide to use "CAML Query"

Spquery Object.  Here it gets tricky because you do use the internal name in your Code, if there are any Spaces you got to replace them by "_x0020_"

E.G :

  spQuery.ViewFields= string.Format("<FieldRef Name='{0}'/>", field.Replace("", "x0020"));

So if there wasn't any space in the field internal name there was no need to replace.
 

Even though there are just 3 or may be bit more scenarios and which a developer can  code to handle them, I feel there is lot of development effort going behind it, If an environment is created where are these scenarios are taken care of on creation of list (as mentioned above ) then it would reduce the development time drastically. As in , you know in all you list there is no column created with a Space in them, why would ever worry about those scenarios during development and think about configuration scenarios where you are making user pick "title" for a column but in you code you got one variable for each field for Internal name and other variable for each field to store CAML field name. May be I am just going way overboard and hoping to have a perfect share point world , but hey! hope is all we got 

So here the rule I follow :

1. Whenever you are creating a a list or fields ALWAYS assume you have to do some custom development work on that list so you better follow the rule  

2. All sites, lists fields (any god damn thing) on creation should not have a space in  between.

3. After creation of site, list and field with your developer friendly name. Rename or assign title according to your Requirements.

4. Make sure you develop something against that list. :)
 

Leave a Reply