Today I came across rather interesting “feature” in SharePoint 2010. I created a new LookupField programmatically and set the Title to a value. This value was however not used. The internal name was used in the display. The more i
So after some digging in .Net Reflector I came up with this. The setter of the Title property is implemented like this:
public void set_Title(string value) { if (!this.Title.Equals(value, StringComparison.Ordinal)) { if (this.TitleResource != null) { this.TitleResource.Value = value; } if (CultureInfo.CurrentUICulture.LCID == this.Web.UICulture.LCID) { this.SetFieldAttributeValue("DisplayName", value); } else if (this.TitleResource != null) { this.SetFieldAttributeValue("DisplayName", this.TitleResource.GetValueForUICulture(this.Fields.Web.UICulture)); } } }
So the reason why this does not work is that I had a german site collection where and the server was english. In this case is the DiplayName not set. Cool
For comparison how it is implemented in SharePoint 2007:
public void set_Title(string value) { this.SetFieldAttributeValue("DisplayName", value); }
As you can see this could not have happened in the old version.
The solution is as follows. Switch the culture of the CurrentThread to the language of the SPWeb you want to work with.
Or do it through reflection
private static void SetFieldDisplayName(SPFieldLookup continuingProjectLookup, string displayName) { Type baseType = continuingProjectLookup.GetType().BaseType; object obj = baseType.InvokeMember("SetFieldAttributeValue", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, continuingProjectLookup, new object[] { "DisplayName", displayName }); continuingProjectLookup.Update(); }