Monday, January 26, 2015

how to get my Variation data

this is a little control i created to extract all the Variation data i usually need, in this case i write it to the browser since its more client based web, but you can as well put all that data anywhere else.

note that if SPContext is null so is the Variations.Current

protected override void Render(HtmlTextWriter writer)
{
   string iso3 = System.Threading.Thread.
                 CurrentThread.CurrentCulture.
                 ThreeLetterISOLanguageName;

   if (iso3 == "eng")
   {
       writer.Write("<link rel=\"Stylesheet\" href=\"/Style Library/CSS/EN.css\"/>");

   }
   string VariationRoot = string.Empty;
   string VariationRootServerRelativeUrl = string.Empty;

   if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
   {
       foreach (VariationLabel label in Variations.Current.UserAccessibleLabels)
       {
           if (label.IsSource == true)
           {
               VariationRoot = "/" + label.TopWebUrl.Split(new char[] { '/' }).Last();
               break;
           }
       }
   string[] relUrlFragments =
        SPContext.Current.Web.ServerRelativeUrl.Split(new char[] { '/' });

   if (relUrlFragments.Length == 2)
   {
       VariationRootServerRelativeUrl = VariationRoot;
   }
   else
   {
       relUrlFragments[1] = VariationRoot.Replace("/", "");
       VariationRootServerRelativeUrl = string.Join("/", relUrlFragments);
   }
    writer.Write("<script>Cbs.Web = {}; " +
       "Cbs.Web.ISO3 = \"" + iso3 + "\"; " +
       "Cbs.Web.ServerRelativeUrl = \"" +
           SPContext.Current.Web.ServerRelativeUrl + "\"; " +
       "Cbs.Web.VariationRoot = \"" + VariationRoot + "\"; " +
       "Cbs.Web.VariationRootServerRelativeUrl = \"" +
           VariationRootServerRelativeUrl + "\"; " +
       "</script>");
   }
   base.Render(writer);
}

No comments:

Post a Comment