Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to change the DisplayName for a field that belongs to a list. Below is the code that I'm using.

using (SPSite site = new SPSite("http://sp10dev/sites/demo"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList demoList = web.Lists.TryGetList("Test");

        SPField deptField = demoList.Fields["Department"];
        deptField.Title = "Last Department";
        deptField.Update();
    }
}

The above code doesn't works as expected. No exception is throw but field DisplayName is unchanged.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Reference the field that you want to change by its Internal Name (or field id) and then change its Display Name. The field's Internal Name (or field id) is unique within the Site Collection but the its Display Name need not be. You can find out the Internal Name by its field properties or using SharePoint 2010 Manager.

If the field is a custom type then you may use its static name by calling the TryGetFieldByStaticName method. See this post for more details - SharePoint Internal name, Static name, Display name.

Also, reconsider the use of spaces in the field names which has its own issues - SharePoint 2010 Fields or Columns Naming Problem.

You also have to update the list itself (at the end of your code segment).

UPDATE: (revised above code segment - but not tested & may need some tweaking)

using (SPSite site = new SPSite("http://sp10dev/sites/demo")) 
{ 
    using (SPWeb web = site.OpenWeb()) 
    { 
        SPList demoList = web.Lists.TryGetList("Test"); 
        SPField deptField = demoList.Fields.GetFieldByInternalName("Department");
        deptField.Title = "Last Department"; 
        deptField.PushChangesToLists = true;   // *** push to list
        deptField.Update(true);            // *** using Update(true) updates list & DB
        demoList.Update();            // *** update list
    } 
} 
share|improve this answer
    
Thanks for the answer, but the problem that I have is not related to find the field by its name. I can, in fact, get the reference to the field, but when I set the Title, which is the Display Name of this field, nothing happens. Field's Display Name remain unchanged. –  Lester Apr 17 '12 at 20:38
    
Ok.. did you try with the two new lines (see // *** comments above to push/update list) –  Supriyo SB Chatterjee Apr 17 '12 at 20:45
    
are these fields being used also in a ContentType(s) ? Also, is your code segment part of an event handler or some privileged code execution? if so, wrap the code segment with an 'AllowUnsafeUpdates' wrapper.. –  Supriyo SB Chatterjee Apr 17 '12 at 20:46
    
Yes, I tried all that you suggest. I finally found the source of the problem, and it is related with an strange behavior when CultureUI for the current thread doesn't match to the SPWeb's CultureUI. My SharePoint installation is in English and I'm working on a site with an Spanish culture. More information about this issue here vojtan.wordpress.com/2010/10/28/… –  Lester Apr 17 '12 at 22:46
    
the programmatic field/list access & updates are a common issue but the one you described (with Culture settings) appears to be a "bug".. –  Supriyo SB Chatterjee Apr 18 '12 at 17:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.