Tuesday, April 23, 2013

SharePoint 2013 Multilingual User Interface (MUI) Switcher

In SharePoint Foundation 2010, when someone navigates to a multilingual website, the website uses the Accept-Language header that the client browser sends with the HTTP request to determine the language in which to render the user interface. If the website does not support any of the languages specified by the browser, the default language is used as the display language
A multilingual website also displays a drop-down menu in the upper-right corner of the page, next to the user's name, where users can select a display language. When someone selects a language that is different from the current display language, the website switches to the new language. The user's preference is persisted in a cookie that is dropped on the client computer. The website gets the user's language preference from the cookie on subsequent visits to the site.



But In SharePoint 2013  the language switcher drop down is removed ... So We have tried to implement our own switcher with same concept in SharePoint 2010 - Reference the Changing the Display Language  - But trying to add the JavaScript was useless.

So we tried another approach ... Using an HTTP Module which interrupts the request in a very early stage - Reference this Page to see how to create and apply an HTTPModule to SharePoint  - .

The trick is the new SharePoint model for changing the display language  is by checking the user's language preferences ... Then adding those languages to the request header in the Accept-Language tag i.e. ar-SA,en-US or buy the language preferences the user configures in his SharePoint user profile. 
  
English Preference Only

Arabic then English Preference 

OK ... we will not reinvent the wheel ... The game plan is to interrupt the request and check for the cookies value - Assuming we have a cookie  preserving the current selected language - Adding the selected language at the to the request header using an HTTP Handler , Then setting the current thread culture to the selected language, By implementing the PreSendRequestHeaders event handler.

UPDATE: Thanks to Suleman, many people was facing some issues in this approach, He found using the PreRequestHandlerExecute event handler solves all issues

e.g context.PreRequestHandlerExecute +=context_PreRequestHandlerExecute;


Following is the code we used for achieving the above scenario by setting the language to arabic:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading.Tasks;
using System.Threading;
 
namespace MUISwitcher
{
    class HTTPSwitcherModule : IHttpModule
    {
        #region IHttpModule Members
 
        public void Dispose()
        {
        }
 
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute +=context_PreRequestHandlerExecute;
        }
 
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication httpApp = sender as HttpApplication;
            HttpContext context = httpApp.Context;
            string httpUrl = context.Request.Url.ToString();
 
            //TODO:Get the selected value for the current culture form the cookie i.e. ar-SA and 
            //set the Header and the CurrentCulture to the aquired value
 
            var lang = context.Request.Headers["Accept-Language"];
 
            if (!lang.Contains("ar-SA"))
                context.Request.Headers["Accept-Language"] = "ar-SA," + context.Request.Headers["Accept-Language"];
 
 
            var culture = new System.Globalization.CultureInfo("ar-SA");
 
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
 
        #endregion
    }
}

Sunday, April 14, 2013

Publishing Page Layout Image Field Disappears when the page is Published

I was facing this problem that when Publishing Page Layout the Image Field Disappears when the page is Published and I found out that the page layout I was using was not associated with the custom content type, as I was using SharePoint designer to copy the layout from one environment to another one, So I opened the master page gallery and edited the properties for this layout and re-associated it with the content type , and everything is working OK right now