n
nWhile working with SharePoint Publishing sites we usually come across using Variations, and sometimes we want to work with them programmatically
n
nas Variations are the outcome of SharePoint Publishing Infrastructure , so of course we need to use SharePoint Publishing APIs .. Microsoft.SharePoint.Publishing
n
none can use simple single line of code to get variation labels programmatically
n
nReadOnlyCollection<VariationLabel> _all = Variations.Current.UserAccessibleLabels;
n
nabove method gives a read only collection of VariationLabels for site , but wait ..
n
nthis 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
n
nbut 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
n
nWell then here is something that is what I got when I looked in buddy reflector
n
nSharePoint 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
n
nSharePoint internally maintains a hidden list where all of these variations are kept, isn’t this sound good?
n
nso approach is fairly simple , you just need to get the list and query and that’s all
n
nhere is sample code
n
nnote : VariationLabelEntity is custom entity class
n
nReference: Waldek Mastykarz
n
n
n
nclass Program
n
n{
n
n private static SPList _variationsList = null;
n
n private static DataTable _allLabels = null;
n
n private static List<VariationLabelEntity> _varLabels = null;
n
n
n
n static void Main(string[] args)
n
n {
n
n try
n
n {
n
n using (SPSite site = new SPSite(“http://YourSite”))
n
n {
n
n using (SPWeb web = site.RootWeb)
n
n {
n
n if (PublishingWeb.IsPublishingWeb(web))
n
n {
n
n string _listIdString = web.AllProperties[“_VarLabelsListId”].ToString();
n
n
n
n if (!string.IsNullOrEmpty(_listIdString))
n
n {
n
n Guid _listId = new Guid(_listIdString);
n
n
n
n _variationsList = web.Lists[_listId];
n
n
n
n if (_variationsList != null)
n
n {
n
n SPQuery query = new SPQuery();
n
n query.Query = @”<Where><IsNotNull><FieldRef Name=’Title’ /></IsNotNull></Where>”;
n
n query.ViewFields = “<FieldRef Name=’Title’/><FieldRef Name=’Language’ /><FieldRef Name=’Locale’ /><FieldRef Name=’Top_x0020_Web_x0020_URL’ />”;
n
n
n
n _allLabels = _variationsList.GetItems(query).GetDataTable();
n
n }
n
n
n
n if (_allLabels != null)
n
n {
n
n _varLabels = new List<VariationLabelEntity>();
n
n foreach (DataRow row in _allLabels.Rows)
n
n {
n
n string _topWebUrl = row[“Top_x0020_Web_x0020_URL”].ToString();
n
n string[] _splits = null;
n
n if (_topWebUrl.Contains(‘,’))
n
n {
n
n _splits = _topWebUrl.Split(‘,’);
n
n _topWebUrl = _splits[0];
n
n }
n
n
n
n _varLabels.Add(new VariationLabelEntity
n
n {
n
n Label = row[“Title”].ToString(),
n
n Language = row[“Language”].ToString(),
n
n Locale = row[“Locale”].ToString(),
n
n TopWebUrl = _topWebUrl,
n
n });
n
n }
n
n }
n
n
n
n if (_varLabels != null && _varLabels.Count > 0)
n
n {
n
n foreach (VariationLabelEntity label in _varLabels)
n
n {
n
n Console.WriteLine(label.Label + “..” + label.Language + “..” + label.Locale + “..” + label.TopWebUrl);
n
n }
n
n }
n
n }
n
n }
n
n }
n
n }
n
n }
n
n catch (Exception ex)
n
n {
n
n Console.WriteLine(ex.Message);
n
n }
n
n
n
n Console.ReadLine();
n
n }
n
n }
n
n
n
npublic class VariationLabelEntity
n
n{
n
n public string Label
n
n { get; set; }
n
n
n
n public string TopWebUrl
n
n { get; set; }
n
n
n
n public string Language
n
n { get; set; }
n
n
n
n public string Locale
n
n { get; set; }
n
n}
n