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

Everybody loves renaming columns. The title column is a favorite of mine and I can't seem to rename it via code:

SPList list = web.Lists.TryGetList("MyList");
Guid guid = new Guid("fa564e0f-0c70-4ab9-b863-0177e6ddd247"); //Title column
SPField field = list.Fields[guid];
field.Title = "Testing";
field.Update();
list.Update();

The title column always stays the title column. Of course I don't want to rename the InternalName or anything crazy, but since you can't remove the column it would be nice if it actually had a meaningful name in my SP context.

So the question in short: How to rename the title column/field in a document library using code (that means C# - not XML)?"

PS: Of course it works via the UI. Go to document library settings > Columns > Title and you can change the name to your liking (this is updated on all views then)


Further investigation shows that it doesn't even work with the SchemaXML:

string schema = field.SchemaXML;
schema.Replace("Title", "NewNameHere");
field.SchemaXml = schema;
field.Update();
list.Update();

Still the field's display name is "Title.


More testing code for you (again this doesn't change anything for me):

Guid listGuid = web.Lists.Add("TestListNow", "", SPListTemplateType.DocumentLibrary);
SPList list = web.Lists[listGuid];

SPField field = list.Fields[new Guid("fa564e0f-0c70-4ab9-b863-0177e6ddd247")];
field.Title = "FieldTitle";
field.TitleResource.SetValueForUICulture(new CultureInfo(1031), "Culture1031");
field.TitleResource.SetValueForUICulture(new CultureInfo(1033), "Culture1033");
field.TitleResource.Update();
field.Update();
list.Update();

The title field stays "Titel" (German title). When going in the same list via the UI I can change the column name...


Working Code:

using (SPSite site = new SPSite("http://myurl.com"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.lists.TryGetList("TestList");
        SPField field = list.Fields.GetFieldByInternalName("Title");
        field.Title = "NewTitle";
        field.Update();
        field.TitleResource.SetValueForUICulture(new CultureInfo(1031), "NewTitle");
        field.TitleResource.Update();
    }
}
share|improve this question
    
Are u able to fix this issue as I am also encountrying the , the title of coulumn changes in second time code execution and that to with changed name. Please help –  user10010 Aug 10 '12 at 11:41

4 Answers 4

up vote 9 down vote accepted

That could be due to localization. In that case, you should also set TitleResource property:

field.TitleResource.SetValueForUICulture(new CultureInfo(1033), "My title");

Actually, I've done renaming of Title plenty of times, and I can't see any difficulties there. Your SchemaXml approach probably didn't work for the same localization reason.

By the way, Title column can be easily deleted, just set AllowDeletion property to false before deleting it: although, it is not recommended, because you will lose some logging features and standard workflows functionality.

share|improve this answer
    
Localization is a very good Idea. I tried what you suggested with German locale (SP is German installed) as well as 1033. I also did a field.TitleResource.Update() - all to no avail. When looking at he Schema there isn't anything suggesting localization, the field has the attributes: Type, Name, ShowInNewForm, DisplayName, Sealed, SourceID, StaticName, ColName. In the end it is just a plain old Doclib with some custom fields added. –  Dennis G Nov 28 '11 at 17:35
    
according to my working code: after setting TitleResource and field.TitleResource.Update() I have also a call to field.Update() - have you tried this? –  Andrey Markeev Nov 28 '11 at 18:00
    
To represent localized field schema, they have separated property, SchemaWithResourceTokens - but this property is readonly. What you can try with SchemaXml, is setting it in different locales contexts, or passing resource tokens into it. But actually I haven't dealed with this approach, because for me SetValueForUICulture method works like a charm. –  Andrey Markeev Nov 28 '11 at 18:05
    
I got to work now! But only when getting the field via .GetFieldByInternalName("Title"). Yes I pretty much update everything. Last weird behavior: The name change only applies after the second time I run the code. E.g. I change the title to "FirstTitle" --> "Titel" remains. I run the code a second time, now using "SecondTitle" --> column is renamed to "FirstTitle". Now I run the code a third time "ThirdTitle" --> "SecondTitle" is applied... –  Dennis G Nov 28 '11 at 18:11
    
hmmm.. could you try this sequence: field.Title = 'something';, then field.Update();, then field.TitleResource.SetValueForUICulture(...);, and finally, field.TitleResource.Update();. Exactly this sequence, no any other updates please. –  Andrey Markeev Nov 28 '11 at 18:28

None of the code above worked for me (SP Foundation 2013). I did it like this:

$webUrl = "http://localhost:80"
$newTitle = "New Title for the field"

$w = Get-SPWeb $webUrl
$ct = $w.ContentTypes["My Content Type Name"]
$fl = $ct.FieldLinks["MyColumn"]
$fl.DisplayName = $newTitle
$ct.Update($true)

$w.Dispose()
share|improve this answer
1  
This question was specifically about the Title column not about just any column $fl = $ct.FieldLinks["MyColumn"]. –  Dennis G Aug 21 at 11:35
    
You right, it's more generic approach for custom column. Thought it might be useful for some1(like me), that was redirected here from Google's "rename field title" request. –  dbardakov Aug 21 at 12:22

It's a bit different. I wrote a blogpost about it here: http://carolinepoint.wordpress.com/2013/06/28/add-a-multilingual-field-to-sharepoint-through-code/

So in short: You want to set the Thread.Current.CurrentUICulture to the culture of the language you which to change. Then change them with field.TitleResource.SetValueForCulture or field.DescriptionResource.SetValueForCulture and call the field.TitleResource.Update() or the field.DescriptionResource.Update()

share|improve this answer
    
While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. [moderator] –  SPDoctor Jun 29 '13 at 14:18

I only tried this in PowerShell, where I can confirm that SetValueForUICulture() doesn't persist the value (or rather it looks like it is set in the database, but GetValueForUICulture() retrieves the old value even though the internal property ResxBased is set to false).

What worked for me was to not use SetValueForUICulture() but instead set the title property after switching the CurrentUIThread culture:

[Threading.Thread]::CurrentThread.CurrentUICulture = "da-DK"
$title = $list.Fields.GetFieldByInternalName("Title")
$title.Title = "My Danish Title"
$title.Update($true) #true=pushChangesToList

This will effectively update title field, and all the related Title "shadow fields".

I also saw it working if I assigned TitleResource to a variable, and first updated that, and then the title directly:

$tr = $title.TitleResource
$tr.SetValueForUiCulture($web.UICulture,"My Title")
$tr.Update()

$title.Title = "My Title"
$title.Update($true)

which really doesnt make much sense. I guess some MUI flag gets triggered by SetValueForUiCulture, but SPUserRessource.Update() seems rather buggy!

share|improve this answer
    
Very good research. –  Dennis G Oct 9 '12 at 9:02

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.