How to programmatically retrieve the root site of a Variation

The Admin UI of the site collection provides easy access to the variation settings. But the object model does not provide easy access to these settings. There are many scenarios where it would be (e.g.) very interesting to know which site is the root site of the variation.

The solution for this problem is Reflection. Using Reflection it is possible to access the VariationSettings object which provides easy access to this information.

Here is some sample code:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace StefanG.Tools
{
    class DumpVariationSettings
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite(“http://url-to-site-collection/”);

            // Get Assembly hosting the hidden VariationSettings class 
            Assembly a = typeof(PublishingWeb).Assembly;

            Type[] types = a.GetTypes();
            Type VariationSettingsType = null;

            // Find VariationSettings type 
            foreach (Type t in types)
            {
                if (t.FullName == “Microsoft.SharePoint.Publishing.Internal.VariationSettings”)
                {
                    VariationSettingsType = t;
                    break;
                }
            }

            // Instantiate a VariationSettings object
            ConstructorInfo ci = VariationSettingsType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, 
                                       nullnew Type[] { typeof(SPSite), typeof(bool) }, null);
            object VariationSettings = ci.Invoke(new object[] { site, false }); 

            // Retrieve the URL of the variation root

            PropertyInfo pi = VariationSettings.GetType().GetProperty(“RootPublishingWebUrl”
                                       BindingFlags.NonPublic | BindingFlags.Instance );
            string Url = pi.GetValue(VariationSettings, nullas string

            // Alternatively retrieve the PublishingWeb of the variation root

            PropertyInfo pi2 = VariationSettings.GetType().GetProperty(“RootPublishingWeb”
                                       BindingFlags.NonPublic | BindingFlags.Instance);
            PublishingWeb pubWeb = pi2.GetValue(VariationSettings, nullas PublishingWeb; 
        }
    }
}

6 Comments


  1. Would it be easier to create a PublishingWeb with the static GetPublishingWeb() Method like this

    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Web);

    Reply

  2. Hi David,

    you missed the point. I’m not trying to get the publishing web for the current web. The code above will give you the web which is configured as the rootweb of the variation in a give site collection.

    Cheers,

    Stefan

    Reply

  3. Why not compare the ServerRelativeURLs?

    SPSite site = SPContext.Current.Site;

               SPWeb currentWeb = SPContext.Current.Web;

               string strWebURL;

               foreach (SPWeb web in site.RootWeb.Webs)

               {

                   strWebURL = web.ServerRelativeUrl;

                   if (currentWeb.ServerRelativeUrl.StartsWith(strWebURL))

    // this is the VariationRoot

    SPWeb.Webs returns only the direct children Websites, so if your variation starts deeper, don’t use SPSite.RootWeb …

    Reply

  4. Hi Markus,

    that will not help you if you don’t know where the root is.

    The code above will get the configured variation root without a need to have any background info at which level it might be.

    So you can write code that works correct in various different site collections with different variation root sites.

    Cheers,

    Stefan

    Reply

  5. Hi Ashish,

    not sure what you mean. The code above will return the variation root site in the "pubWeb" variable and the Url in the Url variable.

    Cheers,

    Stefan

    Reply

Leave a Reply to Stefan Goßner Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.