Friday, September 18, 2015

Localize web part titles via client object model in Sharepoint

Starting with 2010 version Sharepoint has Multilingual UI feature which allows to dynamically switch language used for UI elements on the site (depending on settings this language may be determined by language preferences set in user profile or language http header configured in browser). This feature is also supported in Sharepoint 2013 and Sharepoint Online. Programmatically you may add different translations using special properties. E.g. if you need to localize site title you need to use SPWeb.TitleResource property.

The problem is that client object model has limited support of localizations UI elements. E.g. Microsoft.SharePoint.Client.dll of version (on-premise) doesn’t have these properties at all. The same assembly, but of vesion (online) has the following properties:

  • Web.TitleResource
  • List.TitleResource
  • Field.TitleResource
  • ContentType.NameResource

These properties can be used for adding localizations to appropriate Sharepoint objects. Of course in order to take effect you need to enable MUI on your site. It can be done in Site settings > Language settings:


In this example we enabled alternate Finnish language (language id = 1035) for our site.

The problem however that there is no similar property for web part title. However in Sharepoint in most cases web parts are most often seen by end users than other elements. So it would be good to have a mechanism for localizing web part titles. It can be done by the following PowerShell script:

   1: $localeId = 1035
   3: function ProcessWebParts($context, $page) {
   4:   $manager = $page.GetLimitedWebPartManager(
   5: [Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
   6:   $pageWebParts = $manager.WebParts
   7:   $context.Load($pageWebParts)
   8:   ExecuteLocalizedQuery $context $localeId
  10:   $pageWebParts | foreach {
  11:     $properties = $_.WebPart.Properties
  12:     $context.Load($properties)
  13:     ExecuteLocalizedQuery $context $localeId
  15:     $properties["Title"] = $properties["Title"] + $localeId.ToString()
  16:     $_.SaveWebPartChanges()
  17:   }
  18: } 
  20: function ExecuteLocalizedQuery($context, $localeid) {
  21:     $context.PendingRequest.RequestExecutor.WebRequest.Headers["Accept-Language"] =
  22: (new-object System.Globalization.CultureInfo($localeid)).Name
  23:     $context.ExecuteQuery()
  24: }
  27: $currentDir = Convert-Path(Get-Location)
  28: $dllsDir = resolve-path($currentDir + "\dlls")
  30: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  31: "Microsoft.SharePoint.Client.dll"))
  32: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  33: "Microsoft.SharePoint.Client.Runtime.dll"))
  34: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  35: "Microsoft.SharePoint.Client.Taxonomy.dll"))
  36: [System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($dllsDir,
  37: "Microsoft.Online.SharePoint.Client.Tenant.dll"))
  39: $siteURL = ""
  40: $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)    
  41: $web = $context.Web
  42: $site = $context.Site
  43: $context.Load($web)
  44: $context.Load($site)
  45: ExecuteLocalizedQuery $context $localeId
  47: $page = $web.GetFileByServerRelativeUrl("/Pages/default.aspx")
  48: $context.Load($page)
  49: ExecuteLocalizedQuery $context $localeId
  51: ProcessWebParts $context $page

The key element here is ExecuteLocalizedQuery function (lines 20-24). In this function we pass locale id to ClientContext.PendingRequest.RequestExecutor.WebRequest.Headers[“Accept-Language”]. As result our code is executed as it would be called from UI with used Finnish language. Now if we open site with Finnish language all web parts on default.aspx page will have titles with “1035” suffix. But when we will switch to default English language they will have initial values. The only problem which I found at the moment is that if we will use this script with default English language (language id = 1033), it will override also all other translations regardless of the fact that in Site settings > Language settings Overwrite translations setting is set to No (see above). But this is not a big problem because this script is needed first of all for adding translations for alternate languages.

Hope that this information will be helpful.

No comments:

Post a Comment