Wednesday, November 16, 2011

Working with SharePoint Multilingual Titles Programatically

Last week I was doing some R&D on SharePoint Multilingual user interface and hence thought to share some learning experience which I got.


Background:
SharePoint 2010 ships with the new feature called Multilingual User Interface, which is helpful for displaying the content in the web site in various languages depending upon user selection. I will not dive in to the much details of it as many of good posts are available on the same. Some examples can be found Here and Here.
Just a basic rule of this feature is, it works on the UI culture of the current thread, and shows content to user in different languages.

Problem:
So far so good, SharePoint 2010 manages to translate all link titles, list, libraries titles, site columns titles, content type’s titles and many more things which are out of the box. So when user selects a different language in MUI selector menu then he or she is able to see the OOB translated content very well , but what happens when we have our custom lists, list web parts, custom site columns, content types ? Those don’t get translated by default.

Solution:
One has said very well, there is always a solution J and this line cheers me up. So to overcome this kind of scenario there are two ways.

1.  Manual Approach: one has to go to each and every created custom lists, site columns, and content types and update titles of the same in different languages by changing languages every time using MUI selector. Though this sounds easy but takes lots of efforts when you have sites already provisioned and there is lots of such custom data.

2. Programmatic Approach: I always love such approaches and so I always try to find way to do them as they makes life easy by just few clicks.

A property “TitleResource” is available now with some SharePoint objects which is of type SPUserResourceclass, with which you can get or set the titles of webs, lists, site columns, content types for multiple language cultures.
Following example shows that how we can get or set the multilingual values of a site’s title for German UI culture.



using (SPSite site = newSPSite("http://YourSite"))
{
using (SPWeb _web = site.OpenWeb())
  {
Console.WriteLine(string.Format("{0}-{1}", "Title", _web.Title));

SPUserResource _userResource = _web.TitleResource;
if (_userResource != null)
    {
       _web.AllowUnsafeUpdates = true;
       _userResource.SetValueForUICulture(newSystem.Globalization.CultureInfo(1035), "German Title");
       _userResource.Update();
       _web.AllowUnsafeUpdates = false;

Console.WriteLine("{0}-{1}-{2}", "Updated Title for Culture", "German", _userResource.GetValueForUICulture(newSystem.Globalization.CultureInfo(1035)));

}
  }
}
Console.ReadLine();



No comments:

Post a Comment