How to get all Variation Labels for SharePoint Site



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; }
}

Up Next
    Ebook Download
    View all
    Learn
    View all