Dennis G
Dennis G

Reputation: 21778

How to rename title column in Document Library using code?

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 (the DisplayName at least). 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.

Upvotes: 3

Views: 2533

Answers (1)

Dennis G
Dennis G

Reputation: 21778

Localization is the issue. I need to change the TitleResource additional to the Title:

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

As explained here.

Upvotes: 3

Related Questions