/// <summary> /// Gets all variation labels in a site collection. This method is safe when called in console. /// </summary> /// <param name="publishingSite">Site collection.</param> /// <param name="includePending">Whether to return pending variation labels.</param> /// <returns>A read-only collection of all variation labels.</returns> public static ReadOnlyCollection <VariationLabel> GetVariationLabels(this PublishingSite publishingSite, bool includePending) { if (GetVariationLabelMethod == null) { throw new MissingMethodException("GetVariationLabel"); } List <VariationLabel> collection = new List <VariationLabel>(); publishingSite.Site.WithElevatedPrivileges(elevatedSite => { string variationsListId = (string)elevatedSite.RootWeb.AllProperties["_VarLabelsListId"]; if (!String.IsNullOrEmpty(variationsListId)) { SPList variationsList = elevatedSite.RootWeb.Lists[new Guid(variationsListId)]; CamlExpression queryExpr = Caml.IsNotNull(SPBuiltInFieldName.Title); if (!includePending) { queryExpr &= Caml.IsNotNull("Top_x0020_Web_x0020_URL"); } SPQuery query = new SPQuery { Query = queryExpr.ToString() }; foreach (SPListItem listItem in variationsList.GetItems(query)) { VariationLabel label = GetVariationLabelMethod.Invoke <VariationLabel>(null, listItem); collection.Add(label); } } }); return(collection.AsReadOnly()); }
/// <summary> /// Initializes a new instance of the <see cref="CacheVariationLabel"/> class. /// </summary> /// <param name="variationLabel">The variation label.</param> public VariationLabelInfo(VariationLabel variationLabel) : this() { this.Title = variationLabel.Title; this.DisplayName = variationLabel.DisplayName; this.IsSource = variationLabel.IsSource; this.Language = TryParseCulture(variationLabel.Language); this.Locale = TryParseCulture(variationLabel.Locale); this.TopWebUrl = new Uri(variationLabel.TopWebUrl); }
/// <summary> /// Initializes a new instance of the <see cref="CacheVariationLabel"/> class. /// </summary> /// <param name="variationLabel">The variation label.</param> public VariationLabelInfo(VariationLabel variationLabel) { this.FlagControlDisplayName = variationLabel.DisplayName; this.IsSource = variationLabel.IsSource; this.Language = variationLabel.Language; this.Locale = TryParse(variationLabel); this.Title = variationLabel.Title; this.TopWebUrl = new Uri(variationLabel.TopWebUrl); }
private void InitializeObject(SPWeb web) { SPSite cachedSuperUserSite = null; if (HttpContext.Current != null) { cachedSuperUserSite = HttpContext.Current.Items["SuperUserSite"] as SPSite; if (cachedSuperUserSite != null && cachedSuperUserSite.ID != web.Site.ID) { HttpContext.Current.Items["SuperUserSite"] = null; } } try { if (PublishingWeb.IsPublishingWeb(web)) { PublishingWeb currentWeb = PublishingWeb.GetPublishingWeb(web); VariationLabel currentLabel = GetVariationLabel(currentWeb); if (currentLabel != null) { this.IsVariationWeb = true; this.IsSource = currentLabel.IsSource; this.TopWebServerRelativeUrl = new Uri(currentLabel.TopWebUrl).AbsolutePath; this.VariationLabel = currentLabel.Title; this.PagesListName = currentWeb.PagesListName; int lcid; if (Int32.TryParse(currentLabel.Locale, out lcid)) { this.Culture = CultureInfo.GetCultureInfo(lcid); } else { this.Culture = CultureInfo.GetCultureInfo(currentLabel.Language); } } } if (!this.IsVariationWeb) { this.TopWebServerRelativeUrl = web.Site.ServerRelativeUrl; this.VariationLabel = String.Empty; this.PagesListName = "Pages"; this.Culture = web.UICulture; } } finally { // avoid messing up calls to publishing web API in the same HTTP request (FileNotFoundException) // where SuperUserSite should be reserved for current site collection // https://sharepoint.stackexchange.com/questions/83242 if (HttpContext.Current != null && (cachedSuperUserSite == null || cachedSuperUserSite.ID != web.Site.ID)) { HttpContext.Current.Items["SuperUserSite"] = cachedSuperUserSite; } } }
private static int TryParse(VariationLabel variationLabel) { int number; var result = int.TryParse(variationLabel.Locale, out number); // If the locale is not parsable, we use the Language property. if (result) { return(number); } else { return(new CultureInfo(variationLabel.Language).LCID); } }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); try { PublishingPage publishingPage; PublishingPage targetPage; string currentLang = ""; if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("/eng/")) { currentLang = "en"; } else { currentLang = "fr"; } // Figure out what variation we are in and link back to the other language using the if (SPContext.Current.ListItem != null && PublishingPage.IsPublishingPage(SPContext.Current.ListItem)) { publishingPage = PublishingPage.GetPublishingPage(SPContext.Current.ListItem); VariationLabel label = publishingPage.PublishingWeb.Label; //repass the querystring string queryString = ""; if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString.ToString())) { queryString = "?" + HttpContext.Current.Request.QueryString.ToString(); } // controls on the page can override the query string parameter if needed if (this.Page.Master != null) { Master_Pages.WETIntranetPublishingMaster masterPage = (Master_Pages.WETIntranetPublishingMaster) this.Page.Master; if (!String.IsNullOrEmpty(masterPage.LanguageFlipQueryString)) { queryString = masterPage.LanguageFlipQueryString; } } if (label == null) { // no variations... use the current users local string langlabel = HttpContext.GetGlobalResourceObject("WET", "OtherLanguageText", SPContext.Current.Web.Locale).ToString(); string Languagecontrol = ""; if (currentLang.Equals("en")) { Languagecontrol = "<a href=\"" + publishingPage.Uri.AbsoluteUri + queryString + "\" lang=\"" + currentLang + "\" xml:lang=\"" + currentLang + "\" onclick=\"javascript:OnSelectionChange(1036); return false;\"><span>" + langlabel + "</span></a>"; } else { Languagecontrol = "<a href=\"" + publishingPage.Uri.AbsoluteUri + queryString + "\" lang=\"" + currentLang + "\" xml:lang=\"" + currentLang + "\" onclick=\"javascript:OnSelectionChange(1033); return false;\"><span>" + langlabel + "</span></a>"; } this.Controls.Add(new LiteralControl(Languagecontrol)); } else { // handle the variations // find our root publishing web PublishingWeb topPubWeb = publishingPage.PublishingWeb; while (topPubWeb.IsRoot == false) { topPubWeb = topPubWeb.ParentPublishingWeb; } // iterate through the variation urls and find the lable from the other language foreach (PublishingWeb aPubWeb in topPubWeb.GetPublishingWebs()) { if (aPubWeb.Label != null && (aPubWeb.Label.Title != publishingPage.PublishingWeb.Label.Title) && ((aPubWeb.Label.Title.ToLower() == "eng") || (aPubWeb.Label.Title.ToLower() == "fra"))) { // then we have the pubweb from the other variation... label = aPubWeb.Label; break; } } if (label == null) { throw new System.ArgumentException(HttpContext.GetGlobalResourceObject("WET", "Error_CantFindPubWebLabel", SPContext.Current.Web.Locale).ToString(), "listItem"); } else { targetPage = publishingPage.GetVariation(label); if (targetPage != null) { string currenturl = HttpContext.Current.Request.Path.ToString().ToLower(); string reverseurl = targetPage.Uri.AbsoluteUri; string langlabel = HttpContext.GetGlobalResourceObject("WET", "OtherLanguageText", SPContext.Current.Web.Locale).ToString(); string propLang = (publishingPage.PublishingWeb.Label.Language.Substring(0, 2) == "en") ? "fr" : "en"; this.Controls.Add(new LiteralControl("<script type=\"text/javascript\">" + System.Environment.NewLine + "function OnSelectionChange2(value){" + System.Environment.NewLine + "var today = new Date();" + System.Environment.NewLine + "var oneYear = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);" + System.Environment.NewLine + "var url = \"" + reverseurl + queryString + "\";" + System.Environment.NewLine + "document.cookie = \"lcid=\" + value + \";path=/;expires=\" + oneYear.toGMTString();" + System.Environment.NewLine + "window.location.href = url;" + System.Environment.NewLine + "}" + System.Environment.NewLine + "</script>")); string lang = publishingPage.PublishingWeb.Label.Language.Substring(0, 2); string controlContent = ""; if (lang == "en") { controlContent = @"<section id=""wb-lng""> <h2>Language selection</h2> <ul class=""list-inline""> <li><a lang=""fr"" href=""" + reverseurl + queryString + @""" onclick=""javascript: OnSelectionChange2(1036); return false;"">Français</a></li> </ul> </section>"; } else { { controlContent = @"<section id=""wb-lng""> <h2>Sélection de langue</h2> <ul class=""list-inline""> <li><a lang=""en"" href=""" + reverseurl + queryString + @""" onclick=""javascript: OnSelectionChange2(1033); return false;"">English</a></li> </ul> </section>"; } } this.Controls.Add(new LiteralControl(controlContent)); } } } } else { //repass the querystring string queryString = ""; if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString.ToString())) { queryString = "?" + HttpContext.Current.Request.QueryString.ToString(); } // controls on the page can override the query string parameter if needed if (this.Page.Master != null) { Master_Pages.WETIntranetPublishingMaster masterPage = (Master_Pages.WETIntranetPublishingMaster) this.Page.Master; if (!String.IsNullOrEmpty(masterPage.LanguageFlipQueryString)) { queryString = masterPage.LanguageFlipQueryString; } } string url = SPContext.Current.Web.Url; // no variations... use the current users local string langlabel = HttpContext.GetGlobalResourceObject("WET", "OtherLanguageText", SPContext.Current.Web.Locale).ToString(); string Languagecontrol = ""; if (currentLang.Equals("en")) { Languagecontrol = "<a href=\"" + url + queryString + "\" lang=\"" + currentLang + "\" xml:lang=\"" + currentLang + "\" onclick=\"javascript:OnSelectionChange(1036); return false;\"><span>" + langlabel + "</span></a>"; } else { Languagecontrol = "<a href=\"" + url + queryString + "\" lang=\"" + currentLang + "\" xml:lang=\"" + currentLang + "\" onclick=\"javascript:OnSelectionChange(1033); return false;\"><span>" + langlabel + "</span></a>"; } this.Controls.Add(new LiteralControl(Languagecontrol)); } } catch (Exception ex) { Logger.WriteLog(ex.Message + " " + ex.StackTrace); } }