DisplayName of SPField is not set

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();
}
About these ads

8 Responses to “DisplayName of SPField is not set”

  1. Vladimir Says:

    Thanks a lot for you post

  2. Jarkko Says:

    Thank you very much!! This solved the problem.

  3. Peter Says:

    Wow! Thanks for this blog post – I’ve spent a few hours struggling to solve this issue. Thanks!

  4. Tibi Says:

    Hi! This was really helpful :-). Thnx.

  5. beatles | Pearltrees Says:

    [...] 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) { if (! DisplayName of SPField is not set « Vojtech Nadvornik's Blog [...]

  6. TuanLe Says:

    It’s a great post! Thank you very much! (y)

  7. “SPField.Title()” via une feature en powershell ne change pas le display name | SP is Dirty Says:

    […] POURQUOI :  http://vojtan.wordpress.com/2010/10/28/displayname-of-spfield-is-not-set/ […]

  8. rolandoldengarm Says:

    Thanks!!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: