3

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.

1 Answer 1

5

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
    } 
} 
5
  • 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
    Commented Apr 17, 2012 at 20:38
  • Ok.. did you try with the two new lines (see // *** comments above to push/update list) Commented Apr 17, 2012 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.. Commented Apr 17, 2012 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
    Commented Apr 17, 2012 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".. Commented Apr 18, 2012 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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