Custom Variation Landing page



If you have gone through part 1 of this series then you understand how Microsoft has implemented their variation landing page and logic to implement a custom variation landing page; I used word custom because we didn't want the user to get redirected using the browser's culture, instead we were using a language property in the user's profile (SharePoint User Profiles) and using that, the user should get redirected to the actual variation site.

Example: suppose a user has his language set to German; the user should get redirected to the German Variation site.

Approach:

Obviously we needed to do some customization but how? We went through MSDN link and got three choices as mentioned in link to create custom landing page, but out of all those we didn't follow the approach to edit OOB VariationRootLanding user control, because if you do so then you need to do those customizations on each web front end and any service pack may override your changes.

So we decided to use what Microsoft uses, such as creating a custom user control, creating a custom page layout for the page our user control will be in and a custom publishing page using our page layout, and after all of this , set this page as the welcome page of the root site.

See how:

Creating Custom User Control - CustomVariationLandingRoot user control

Microsoft has used the inline coding approach to implement their control; I don't know why.

We created a user control and added this code to code behind.

What this code basically does is, maintains a Dictionary object consisting of VaraitionLabel's language as key and VariationLabels's web url as value, and then tries to get the user's language and uses that as the key to do a search in the dictionary object. If it is found then we get the variation web url from the value and if not then we are assigning redirection web url as the source label.

I will try to demonstrate this code in a sample webpart; you can add this code to your user control's code behind with some changes in code. I have used hard coding to set the user's language to German; you can write your own code to get the user's language from the user's profile or as you need to or prefer.

private ReadOnlyCollection<VariationLabel> _allLabels = null;
private Dictionary<string, string> _languageToUrl = null;
private string _language = string.Empty;
string _sourceUrl = string.Empty;
string _redirectUrl = string.Empty;

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  try
  {
   using (SPSite site = new SPSite(SPContext.Current.Site.ID))
   {
     using (SPWeb web = site.RootWeb)
     {
      // This Tries to Get all User Accessible Labels
      //and for which Variation Hierarchies have been created succesfully

      _allLabels = Variations.Current.UserAccessibleLabels;

      if (_allLabels != null && _allLabels.Count > 0)
      {

       _languageToUrl = new Dictionary<string, string>();

       //Dictionary Initialization

       foreach (VariationLabel _label in _allLabels)
       {
         //Check If Source Label , If yes then set source
         //web url to variable
         if (_label.IsSource)
         {
           _sourceUrl = _label.TopWebUrl;
         }

         //Check whether key exists or not If no then add
         if (!_languageToUrl.ContainsKey(_label.Language))
         {
           //Create CultureInfo From Label

           CultureInfo _cultureInfo = CreateCulture(_label);

           if (_cultureInfo != null)
           {
             string[] _displayLanguage = _cultureInfo.EnglishName.Split('(');

                                           _languageToUrl.Add(_displayLanguage[0].Trim(), _label.TopWebUrl);

           }
          }
         }

         //Searching for Key

         _language = "German";

          _redirectUrl = _sourceUrl;

          if (!string.IsNullOrEmpty(_language))
          {
           if (_languageToUrl.ContainsKey(_language))
           {
             _redirectUrl = _languageToUrl[_language];
           }
          }

           //Standard SP Redirection
           SPUtility.Redirect(_redirectUrl, SPRedirectFlags.Trusted, HttpContext.Current);
          }

          }
         }
        }

        catch (Exception ex)
        {
          this.Controls.Add(new LiteralControl(ex.Message));
        }
        }

//Private Function to get Culture Info from Label
        private CultureInfo CreateCulture(VariationLabel label)
        {
            CultureInfo _ci = null;
            if (label != null)
            {
                string _localeId = label.Locale;
 
                if (!string.IsNullOrEmpty(_localeId))
                {
                     _ci = new CultureInfo(Convert.ToInt32(_localeId), false);
               
}
            }

             return _ci;
        }

After all the code is done, we created a custom page layout and placed this user control directly, then the final step was to create a custom publishing page using page layout we made; after that we just set this newly created page as the welcome page of the root site.

How to set a welcome page for a site, I already posted in this so you can refer to that.

And that's all .. I hope this helps someone.