My new blog

My blog has moved to www.davehunter.co.uk/blog see you there.

Tuesday 29 May 2007

MOSS Variations …. Customising the landing page logic

The out-of-the-box variation landing page functionality in SharePoint 2007 is very flexible (the default language is based on your browser settings), but if you want to provide a more strict route into your site. For example: A site which is predomiately in German would want to show German first and then allow the user to select another language. You can do so by:

1. Setup variations with the default language as the source variation. For example: DE.
2.
Customising the root landing page, for more information http://msdn2.microsoft.com/en-us/library/ms562040.aspx
2.1. I chose option 3 as the best approach as this would only affect one site and I'm not fond of inline code.
2.2. Write a class inheriting from the System.Web.UI.WebControls.WebControl class containing the code below.
2.2. Add the assembly into the GAC and SafeControls in web.config
2.3. Replace the original logic in the VariationRootLandingPage with the custom server control.

You have now customised your multi-lingual site to redirect visitors to your default language (source variation).


#region namespaces
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Publishing;
#endregion

namespace MOSS2007.Sample.Multilingual
{
public class LanguageSwitch : System.Web.UI.WebControls.WebControl
{
#region overriden methods

///


/// Override the onload event to redirect the user to a variation based on where they came from
///

///
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

try
{
// get the current variation labels
ReadOnlyCollection spawnedLabels = Variations.Current.UserAccessibleLabels;

string url = "";

if (spawnedLabels.Count > 0)
{
// loop through the labels to find the source label
foreach (VariationLabel label in spawnedLabels)
{
// get the source variation url
if (label.IsSource)
{
// send the user to the source variation
url = label.TopWebUrl;
break;
}

}
}

// redirect the user to the source variation
SPUtility.Redirect(url, SPRedirectFlags.Default, Context);

}
catch (Exception ex)
{
Context.Response.Write(ex.Message);
Context.Response.Write(ex.StackTrace);
Context.Response.Write(ex.Source);

if (ex.InnerException != null)
{
Context.Response.Write(ex.InnerException.Message);
Context.Response.Write(ex.InnerException.StackTrace);
Context.Response.Write(ex.InnerException.Source);
}
}
}
#endregion
}
}

No comments: