Custom Variation Landing page

n

n

n

nIf you have gone through part 1 of this series then you will understand how Microsoft has implemented their variation landing page and logic

n

nWe had a requirement to implement custom variation landing page, I used word custom because we didn’t want user to get redirected using browser’s culture, instead we were using one language property in user’s profile (SharePoint User Profiles) and using that user should get redirected to actual variation site

n

nExample: suppose a user has his language set as German, and then user should get redirected to German Variation site

n

nApproach:

n

nObviously 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 those all we didn’t follow 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 If any service pack may override your changes

n

nSo, we decided to follow how Microsoft done , like creating custom user control , creating a custom page layout on which our user control will be and a custom publishing page using our page layout , and after all of this , set this page as welcome page of root site

n

nsee how:

n

nCreating Custom User Control – CustomVariationLandingRoot user control

n

n

n

nMicrosoft has followed inline coding approach while they implemented their control I don’t know why

n

nWe created   a user control and added this code to code behind

n

nWhat 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 user’s language and uses as key to do search in dictionary object, If found then we get variation web url as value and If not then we are assigning redirection web url as source label

n

nI will try to demonstrate this code in sample webpart you can add this code to your user control’s code behind with some changes

n

nin code I have used hardcoding for user’s language as German , you can write your own code to get user’s language from user profile’s or according to changes you want

n
n

nprivate ReadOnlyCollection<VariationLabel> _allLabels = null;

n

nprivate Dictionary<string, string> _languageToUrl = null;

n

nprivate string _language = string.Empty;

n

nstring _sourceUrl = string.Empty;

n

nstring _redirectUrl = string.Empty;

n

n

n

nprotected override void OnLoad(EventArgs e)

n

n{

n

n  base.OnLoad(e);

n

n  try

n

n  {

n

n   using (SPSite site = new SPSite(SPContext.Current.Site.ID))

n

n   {

n

n     using (SPWeb web = site.RootWeb)

n

n     {

n

n      // This Tries to Get all User Accessible Labels

n

n      //and for which Variation Hierarchies have been created succesfully

n

n

n

n      _allLabels = Variations.Current.UserAccessibleLabels;

n

n

n

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

n

n      {

n

n

n

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

n

n

n

n       //Dictionary Initialization

n

n

n

n       foreach (VariationLabel _label in _allLabels)

n

n       {

n

n         //Check If Source Label , If yes then set source

n

n         //web url to variable

n

n         if (_label.IsSource)

n

n         {

n

n           _sourceUrl = _label.TopWebUrl;

n

n         }

n

n

n

n         //Check whether key exists or not If no then add

n

n         if (!_languageToUrl.ContainsKey(_label.Language))

n

n         {

n

n           //Create CultureInfo From Label

n

n

n

n           CultureInfo _cultureInfo = CreateCulture(_label);

n

n

n

n           if (_cultureInfo != null)

n

n           {

n

n             string[] _displayLanguage = _cultureInfo.EnglishName.Split(‘(‘);

n

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

n

n

n

n           }

n

n          }

n

n         }

n

n

n

n         //Searching for Key

n

n

n

n         _language = “German”;

n

n

n

n          _redirectUrl = _sourceUrl;

n

n

n

n          if (!string.IsNullOrEmpty(_language))

n

n          {

n

n           if (_languageToUrl.ContainsKey(_language))

n

n           {

n

n             _redirectUrl = _languageToUrl[_language];

n

n           }

n

n          }

n

n

n

n           //Standard SP Redirection

n

n           SPUtility.Redirect(_redirectUrl, SPRedirectFlags.Trusted, HttpContext.Current);

n

n          }

n

n

n

n          }

n

n         }

n

n        }

n

n

n

n        catch (Exception ex)

n

n        {

n

n          this.Controls.Add(new LiteralControl(ex.Message));

n

n        }

n

n        }

n

n

n

n//Private Function to get Culture Info from Label

n

n        private CultureInfo CreateCulture(VariationLabel label)

n

n        {

n

n            CultureInfo _ci = null;

n

n

n

n            if (label != null)

n

n            {

n

n                string _localeId = label.Locale;

n

n

n

n                if (!string.IsNullOrEmpty(_localeId))

n

n                {

n

n                     _ci = new CultureInfo(Convert.ToInt32(_localeId), false);

n

n                }

n

n            }

n

n

n

n            return _ci;

n

n        }

n
n

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

n

nHow to set welcome page of site, I already posted on this so you can refer this link

n

nand that’s all .. I hope this helps someone

n

Leave a Comment