How to Change the Display Name of OOB Column using Object Model

Hello All,

Today I am going to post small and very simple post – “How to change the display name of OOB column using object model”. Since I struggled for some time to change the display name of OOB title column in one of the my custom list I thought I should share this.

In our project we have our custom list and list content type is derived from ITEM content type (0x01). So we have Title column from ITEM content type. But customer want to change the display name of Title column say “Customer Title”. So I thought I will simply do this change in my content and it will work like.

  1. <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Customer Title" Required="TRUE"/> 

But unfortunately it didn’t work. I did some research, some blogs says that change the display name in Schema.xml of list definition in <Fields> section for Title field but for us this is also not a solution since we have created our list using object model in FeatureActivated() of our feature receiver. Again did some research and able to change the display name of OOB title column. We could do this by updating “Title” property of respective SPField instance.

Following is the sample code:

  1. if (list.Fields.ContainsField("Title"))  
  2. {  
  3.    SPField fldTitle = list.Fields["Title"];  
  4.    fldTitle.Title = “My Custom DisplayName”;  
  5.    fldTitle.Update(true);  
  6.   

And following is the full code.

  1. public class MyCustomerEventReceiver : SPFeatureReceiver  
  2. {  
  3.  
  4.     #region Overload Methods  
  5.     public override void FeatureActivated(SPFeatureReceiverProperties properties)  
  6.     {  
  7.         SPSite site = properties.Feature.Parent as SPSite;  
  8.         if (site != null) {  
  9.             SPWeb web = site.RootWeb;  
  10.             SPList list = web.Lists.TryGetList(“My Custom List”);  
  11.             bool flag = web.AllowUnsafeUpdates;  
  12.             if (list == null)  {  
  13.                web.AllowUnsafeUpdates = true;  
  14.                Guid guid = web.Lists.Add(My Custom List, "Custom List", SPListTemplateType.GenericList);  
  15.                           
  16.                list = web.Lists[guid];  
  17.                           
  18.                if (list.Fields.ContainsField("Title"))  
  19.                {  
  20.                    SPField fldTitle = list.Fields["Title"];  
  21.                    fldTitle.Title = “My Custom DisplayName”;  
  22.                    fldTitle.Update(true);  
  23.                           
  24.                }  
  25.                list.Update();  
  26.             }  
  27.         }  
  28.     }  
  29.   } 

Thanks!