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:
Would it be easier to create a PublishingWeb with the static GetPublishingWeb() Method like this
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Web);
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.
Why not compare the ServerRelativeURLs?
SPSite site = SPContext.Current.Site;
SPWeb currentWeb = SPContext.Current.Web;
foreach (SPWeb web in site.RootWeb.Webs)
strWebURL = web.ServerRelativeUrl;
// this is the VariationRoot
SPWeb.Webs returns only the direct children Websites, so if your variation starts deeper, don't use SPSite.RootWeb ...
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.
I have variation urls like http://server/sites/site1/variationrootsite/language1/subweb1/subweb11 and http://server/sites/site1/variationrootsite/language2/subweb1/subweb11. I am at subweb11 of language1. how i can get the variationroot url directly which about the help of above code?
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.