Last active
March 30, 2016 07:45
-
-
Save vgrem/6757a1dab43eb320aa6c to your computer and use it in GitHub Desktop.
How to manage Variations in SharePoint 2013 via CSOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using Microsoft.SharePoint.Client; | |
namespace Publishing | |
{ | |
/// <summary> | |
/// Variations Client | |
/// </summary> | |
class VariationsClient | |
{ | |
public VariationsClient(ClientContext context) | |
{ | |
_labelsList = GetLabelsList(context); | |
} | |
/// <summary> | |
/// Create Variation Label | |
/// </summary> | |
/// <param name="cultureInfo"></param> | |
/// <param name="isSource"></param> | |
public void CreateLabel(CultureInfo cultureInfo,bool isSource) | |
{ | |
var entry = new VariationLabelEntry | |
{ | |
DisplayName = cultureInfo.DisplayName, | |
IsSource = isSource, | |
Locale = cultureInfo.LCID, | |
Language = cultureInfo.Name, | |
Title = cultureInfo.Name, | |
}; | |
var variationWebUrl = string.Format("{0}/{1}", RootWebUrl, (isSource ? string.Empty : cultureInfo.Name)); | |
entry.TopWebUrl = new FieldUrlValue { Url = variationWebUrl }; | |
SaveLabel(entry); | |
} | |
/// <summary> | |
/// Retrieve Variation Labels | |
/// </summary> | |
/// <param name="context"></param> | |
/// <returns></returns> | |
public List<VariationLabelEntry> LoadLabels(ClientContext context) | |
{ | |
var labelItems = _labelsList.GetItems(CamlQuery.CreateAllItemsQuery()); | |
context.Load(labelItems); | |
context.ExecuteQuery(); | |
return labelItems.Select(labelItem => VariationLabelEntry.Parse(labelItem)).ToList(); | |
} | |
/// <summary> | |
/// Get Labels List | |
/// </summary> | |
private static List GetLabelsList(ClientContext context) | |
{ | |
var rootWeb = context.Site.RootWeb; | |
context.Load(rootWeb, w => w.AllProperties, w => w.Url); | |
context.ExecuteQuery(); | |
var varLabelsListId = new Guid(rootWeb.AllProperties[VarLabelsListIdKey].ToString()); | |
return rootWeb.Lists.GetById(varLabelsListId); | |
} | |
/// <summary> | |
/// Save Variation Label | |
/// </summary> | |
/// <param name="entry"></param> | |
public void SaveLabel(VariationLabelEntry entry) | |
{ | |
var labelItem = _labelsList.AddItem(new ListItemCreationInformation()); | |
labelItem[VariationLabelFieldNames.Language] = entry.Language; | |
labelItem[VariationLabelFieldNames.IsSource] = entry.IsSource; | |
labelItem[VariationLabelFieldNames.Locale] = entry.Locale.ToString(CultureInfo.InvariantCulture); | |
labelItem[VariationLabelFieldNames.Title] = entry.Title; | |
labelItem[VariationLabelFieldNames.TopWebUrl] = entry.TopWebUrl; | |
labelItem[VariationLabelFieldNames.DisplayName] = entry.DisplayName; | |
labelItem[VariationLabelFieldNames.HierarchyIsCreated] = false; | |
labelItem[VariationLabelFieldNames.HierarchyCreationMode] = HierarchyCreationMode.PublishingSitesAndAllPages; | |
labelItem[VariationLabelFieldNames.NotificationMode] = false; | |
labelItem.Update(); | |
_labelsList.Context.ExecuteQuery(); | |
} | |
/// <summary> | |
/// Variation Root Web Url | |
/// </summary> | |
public string RootWebUrl | |
{ | |
get | |
{ | |
return Context.Site.RootWeb.Url; | |
} | |
} | |
/// <summary> | |
/// Client Context | |
/// </summary> | |
private ClientContext Context | |
{ | |
get { return _labelsList.Context as ClientContext; } | |
} | |
private readonly List _labelsList; | |
private const string VarLabelsListIdKey = "_VarLabelsListId"; | |
} | |
/// <summary> | |
/// Variation Label Entry | |
/// </summary> | |
class VariationLabelEntry | |
{ | |
/// <summary> | |
/// Convert Label List Item into Variation Label Entity | |
/// </summary> | |
/// <param name="item"></param> | |
/// <returns></returns> | |
public static VariationLabelEntry Parse(ListItem item) | |
{ | |
var label = new VariationLabelEntry | |
{ | |
IsSource = bool.Parse(item[VariationLabelFieldNames.IsSource].ToString()), | |
Language = item[VariationLabelFieldNames.Language].ToString(), | |
Locale = int.Parse(item[VariationLabelFieldNames.Locale].ToString()), | |
Title = item[VariationLabelFieldNames.Title].ToString(), | |
TopWebUrl = (FieldUrlValue) item[VariationLabelFieldNames.TopWebUrl], | |
DisplayName = item[VariationLabelFieldNames.DisplayName].ToString(), | |
HierarchyIsCreated = bool.Parse(item[VariationLabelFieldNames.HierarchyIsCreated].ToString()), | |
NotificationMode = bool.Parse(item[VariationLabelFieldNames.NotificationMode].ToString()), | |
HierarchyCreationMode = item[VariationLabelFieldNames.HierarchyCreationMode].ToString() | |
}; | |
return label; | |
} | |
#region Properties | |
public string DisplayName { get; set; } | |
public bool IsSource { get; set; } | |
public string Language { get; set; } | |
public int Locale { get; set; } | |
public string Title { get; set; } | |
public string Description { get; set; } | |
public FieldUrlValue TopWebUrl { get; set; } | |
public bool HierarchyIsCreated { get; private set; } | |
public bool NotificationMode { get; set; } | |
public bool? IsMachineTranslationEnabled { get; set; } | |
public bool? IsHumanTranslationEnabled { get; set; } | |
public int? MachineTranslationLanguage { get; set; } | |
public int? HumanTranslationLanguage { get; set; } | |
public string HierarchyCreationMode { get; set; } | |
#endregion | |
} | |
public static class VariationLabelFieldNames | |
{ | |
public static string IsSource = "Is_x0020_Source"; | |
public static string Language = "Language"; | |
public static string Locale = "Locale"; | |
public static string Title = "Title"; | |
public static string TopWebUrl = "Top_x0020_Web_x0020_URL"; | |
public static string DisplayName = "Flag_x0020_Control_x0020_Display"; | |
public static string HierarchyIsCreated = "Hierarchy_x0020_Is_x0020_Created"; | |
public static string NotificationMode = "NotificationMode"; | |
public static string IsMachineTranslationEnabled = "IsMachineTranslationEnabledField"; | |
public static string IsHumanTranslationEnabled = "IsHumanTranslationEnabledFieldNa"; | |
public static string MachineTranslationLanguage = "MachineTranslationLanguageFieldN"; | |
public static string HumanTranslationLanguage = "HumanTranslationLanguageFieldNam"; | |
public static string HierarchyCreationMode = "Hierarchy_x0020_Creation_x0020_M"; | |
} | |
public sealed class HierarchyCreationMode | |
{ | |
public static readonly string PublishingSitesAndAllPages = ("Publishing Sites and All Pages"); | |
public static readonly string PublishingSitesOnly = ("Publishing Sites Only"); | |
public static readonly string RootSiteOnly = ("Root Site Only"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment