Pages

Tuesday, February 15, 2011

How to get all Variation Labels for SharePoint Site

While working with SharePoint Publishing sites we usually come across using Variations, and sometimes we want to work with them programmatically
as Variations are the outcome of SharePoint Publishing Infrastructure , so of course we need to use SharePoint Publishing APIs .. Microsoft.SharePoint.Publishing
one can use simple single line of code to get variation labels programmatically
ReadOnlyCollection<VariationLabel> _all = Variations.Current.UserAccessibleLabels;
above method gives a read only collection of VariationLabels for site , but wait ..
this method returns list of labels only when 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 variation hierarchy of some sites is not created successfully
Well then here is something that is what I got when I looked in buddy reflector
SharePoint is platform where most of things are coming from list , where its OOB or while you do your development , and same is the case with Variations
SharePoint internally maintains a hidden list where all of these variations are kept, isn’t this sound good?
so 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; }
}

No comments:

Post a Comment