PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

VariationRootLandig redirect to another Variation Site

Posted by PANVEGA on May 15, 2008

Szenario:

When using the Variation in MOSS you have to manually redirect a user to a specific Sharepoint Variation language Site. When SharePoint is setup to use Variations, the root site collection is no longer used to store pages. Instead, you will have sub sites under the web root representing your variations. In my example I wan to redirect the user by his browsers local language settings. For example in Switzerland you find Italian, German and a Frenc speaking population. One of the variation Site is CH. The goal is to redirect users with fr-CH and it-CH to the Switzerland Variation Site. If MOSS does not find any personal language settings, the user wil be redirected to the default source site.

Important Note:

Notice that the labels are setup to use the culture-region format. YOu can find the list in your web browser sitteings e.g. [fr-CH ]. If you don’t follow this format, the variation logic will not be able to match the client language properly.

Introduction:

If you browse to the Variation home site, Office SharePoint Server 2007 looks up the WelcomePage property of the site and runs the VariationRoot page. Because that page is based on VariationRootPageLayout.aspx, it runs the page layout file. The VariationRootPageLayout page layout contains a reference to a file called VariationsRootLanding.ascx and one user control that is defined in the VariationsRootLanding.ascx file. The Page Layout executes the logic in the VariationsRootLanding.ascx file and redirects the user to a Variation site.

The root welcome page is configured by SharePoint (upon creation of variation labels and site structures) to point to a file called VariationRoot.aspx. This file contains a user control VariationsRootLanding.ascx, which has the code that redirects. The default behavior of the VariationsRootLanding.ascx is to detect the language setting of the client, and try to match it with one of the variation labels.

The default logic used in the VariationsRootLanding.ascx file is to redirect the user to the Variation site that matches the browser’s accept lang, or array of language codes in which the current document is available. The VariationsRootLanding.ascx file is defined in the path \<Program Files>\Common Files\Microsoft Shared Debug\Web Server Extensions\12\Template\ControlTemplates.

If you want the default Variation root landing page logic to redirect the user to a Variation site that is not based on the accept lang, you can do this in one of three ways. Each option has its own advantages and disadvantages, and you should consider them carefully before deciding which approach to use.

More detailed Information http://msdn.microsoft.com/en-us/library/ms562040.aspx. I choosed  To modify the root landing logic to detect when a functioning Web site is present  in my scenario.

Example how to redirect a user to a specific varaition site. This is pretty simple, however it works fine. YOu find the UserControl under the path C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\VariationsRootLanding.ascx

using (SPWeb web = site.OpenWeb())
{
// If matchedUrl is the same as the URL of the Web
// you just opened, then matchedUrl is valid
if (string.Compare(matchedUrl, web.Url, StringComparison.OrdinalIgnoreCase) == 0 && web.DoesUserHavePermissions(SPBasePermissions.Open))
{
//Target URL was valid; return it

string[] browserLanguages = Page.Request.UserLanguages;
if (null != browserLanguages)
{
foreach (string value in browserLanguages)
{
if (value.Contains(“fr-CH”) || value.Contains(“it-CH”))
{
string[] segments = Page.Request.Url.Segments;
//build a proper uri
string protocol = String.Empty;
if (Page.Request.IsSecureConnection)
{
protocol = “https://&#8221;;
}
else
{
protocol = “http://&#8221;;
}
protocol += Page.Request.Url.Host;
int i = 0;
while (segments.Length > i + 2)
{
protocol += segments[i];
i++;
}
matchedUrl = protocol + “CH/Pages/”;
return matchedUrl;
}
}
}
return matchedUrl;
}
else
{
//some Text
}
}

Note: If you wanna use the redirect for intranet and internet you have to add the default.aspx Page e.g. CH/Pages/default.aspx” to the new builded URL. Sharepoint can only solve the URL when the User is authenticated, otherwise an ACCESS DENIED error occured on the screen.

With the Page.Request.UserLanguages you get in an array all local custom lanuages from the browser. Then build your URL string like http://domain:Port/CH/Pages.

Authentication to MOSS:

You can add the following code below at the beginning of your Method in order to check if the user is authenticated.

AuthenticationSection authenticationSection = (AuthenticationSection)ConfigurationManager.GetSection(“system.web/authentication”);

// Ensure Forms Authentication and user is authenticated

if (this.Page.User.Identity.IsAuthenticated && authenticationSection.Mode == AuthenticationMode.Forms)

{ ….}

If the user is not authenticated add for example the following code in the else block as default or do something else with unauthenticated users.

Dictionary<string, string> cultureCodeToUrlMapping = new Dictionary<string, string>();
Dictionary<string, string> cultureCodeStrippedToUrlMapping = new Dictionary<string, string>();
foreach (VariationLabel label in spawnedLabels)
{
if (label.IsSource)
{
sourceLabelUrl = label.TopWebUrl;
}

CultureInfo labelCultureInfo = this.GetLabelCultureInfo(label);
string labelCultureInfoName = labelCultureInfo.Name.ToUpperInvariant();
if (!cultureCodeToUrlMapping.ContainsKey(labelCultureInfoName) || label.IsSource)
{
cultureCodeToUrlMapping.Remove(labelCultureInfoName);
cultureCodeToUrlMapping.Add(labelCultureInfoName, label.TopWebUrl);
string strippedCode = labelCultureInfoName.Split(‘-’)[0];
if (!cultureCodeStrippedToUrlMapping.ContainsKey(strippedCode) || label.IsSource)
{
cultureCodeStrippedToUrlMapping.Remove(strippedCode);
cultureCodeStrippedToUrlMapping.Add(strippedCode, label.TopWebUrl);
}
}
}
if (MatchingPreference.ImpreciseOrderFirst == this.preferenceOrder)
{

matchedUrl = this.GetRedirectTargetUrlImpreciseOrderFirst(
cultureCodeToUrlMapping, cultureCodeStrippedToUrlMapping);
}
else
{
matchedUrl = this.GetRedirectTargetUrlPreciseMatch(
cultureCodeToUrlMapping, cultureCodeStrippedToUrlMapping);
}

Note:

If you have have the same effect, thus using the same direction URL you do not need checking the authentication of a user.

Thats all you have to know.

Other resources:

http://msdn.microsoft.com/en-us/library/ms562040.aspx

http://forums.msdn.microsoft.com/en-US/sharepointcustomization/thread/99b01bd4-9f09-4941-9551-cc42e534c70e/

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: