SIGN UP MEMBER LOGIN:    
ARTICLE

How to get all Variation Labels for SharePoint Site

Posted by Bhushan Gawale Articles | SharePoint February 16, 2011
Getting all Variation Labels for a SharePoint Site.
Reader Level:


While working with SharePoint Publishing sites we usually encounter use of Variations, and sometimes we want to work with them programmatically since Variations are the result of the SharePoint Publishing Infrastructure, so of course we need to use SharePoint Publishing APIs...

Using Microsoft.SharePoint.Publishing it is possible to use a simple single line of code to get variation labels programmatically.

ReadOnlyCollection<VariationLabel> _all = Variations.Current.UserAccessibleLabels;

The method above returns a read only collection of VariationLabels for the site, but wait...
 
This method returns the list of labels only when the variation site hierarchy for a label is created successfully and if a requesting user has permission to access the variation site but sometimes you might need to get all variation labels for a site even if you don't have access to those sites or when the variation hierarchy of the site is not created successfully.

Well then here is something that I found when I looked in our buddy the reflector.

In the SharePoint platform most things are in lists, where its OOB or while you do your development, and that is the case with Variations.

SharePoint internally maintains a hidden list where all of these variations are kept, does that sound good?

So the approach is fairly simple; you just need to get the list and query and that's all.

Here is sample code

Note : VariationLabelEntity is custom entity class

Reference: Waldek Mastykarz

class Program
{
  private static SPList _variationsList = null;
  private static DataTable _allLabels = null;
  private static List<VariationLabelEntity> _varLabels = null;

  static void Main(string[] args)
  {
   try
   {
    using (SPSite site = new SPSite("http://YourSite"))
    {
      using (SPWeb web = site.RootWeb)
      {
       if (PublishingWeb.IsPublishingWeb(web))
       {
         string _listIdString = web.AllProperties["_VarLabelsListId"].ToString();

         if (!string.IsNullOrEmpty(_listIdString))
         {
           Guid _listId = new Guid(_listIdString);

           _variationsList = web.Lists[_listId];

           if (_variationsList != null)
           {
             SPQuery query = new SPQuery();
             query.Query = @"<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where>";
             query.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Language' /><FieldRef Name='Locale' /><FieldRef Name='Top_x0020_Web_x0020_URL' />";

             _allLabels = _variationsList.GetItems(query).GetDataTable();
           }

           if (_allLabels != null)
           {
             _varLabels = new List<VariationLabelEntity>();
             foreach (DataRow row in _allLabels.Rows)
             {
               string _topWebUrl = row["Top_x0020_Web_x0020_URL"].ToString();
               string[] _splits = null;
               if (_topWebUrl.Contains(','))
               {
                _splits = _topWebUrl.Split(',');
                _topWebUrl = _splits[0];
               }

                _varLabels.Add(new VariationLabelEntity
                {
                  Label = row["Title"].ToString(),
                  Language = row["Language"].ToString(),
                  Locale = row["Locale"].ToString(),
                  TopWebUrl = _topWebUrl,
                 });
                }
               }

           if (_varLabels != null && _varLabels.Count > 0)
           {
            foreach (VariationLabelEntity label in _varLabels)
            {
             Console.WriteLine(label.Label + ".." + label.Language + ".." + label.Locale + ".." + label.TopWebUrl);
            }
           }
          }
         }
         }
        }
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
       
}

         Console.ReadLine();
     }
    }

public class VariationLabelEntity
{
        public string Label
        { get; set; }

        public string TopWebUrl
        { get; set; }

        public string Language
        { get; set; }

        public string Locale
        { get; set; }
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor