public void SetDefaultColumnValuesTest() { using (var clientContext = TestCommon.CreateClientContext()) { TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(clientContext); List <DefaultColumnTermValue> defaultValues = new List <DefaultColumnTermValue>(); var defaultColumnValue = new DefaultColumnTermValue(); defaultColumnValue.FieldInternalName = "TaxKeyword"; // Enterprise metadata field, should be present on the list defaultColumnValue.FolderRelativePath = "/"; // Root Folder var term = taxSession.GetTerm(_termId); //clientContext.Load(term, t => t.Id, t => t.Name); //clientContext.ExecuteQuery(); defaultColumnValue.Terms.Add(term); defaultValues.Add(defaultColumnValue); var list = clientContext.Web.Lists.GetById(_listId); list.SetDefaultColumnValues(defaultValues); } }
public void SetDefaultColumnValuesTest() { if (TestCommon.AppOnlyTesting()) { Assert.Inconclusive("Taxonomy tests are not supported when testing using app-only"); } using (var clientContext = TestCommon.CreateClientContext()) { TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(clientContext); List <IDefaultColumnValue> defaultValues = new List <IDefaultColumnValue>(); var defaultColumnValue = new DefaultColumnTermValue(); defaultColumnValue.FieldInternalName = "TaxKeyword"; // Enterprise metadata field, should be present on the list defaultColumnValue.FolderRelativePath = "/"; // Root Folder var term = taxSession.GetTerm(_termId); defaultColumnValue.Terms.Add(term); defaultValues.Add(defaultColumnValue); var testDefaultValue = new DefaultColumnTextValue(); testDefaultValue.Text = "Bla"; testDefaultValue.FieldInternalName = _textFieldName; testDefaultValue.FolderRelativePath = "/"; // Root folder defaultValues.Add(testDefaultValue); var list = clientContext.Web.Lists.GetById(_listId); list.SetDefaultColumnValues(defaultValues); } }
public static void SetDefaultColumnValues(this List list, IEnumerable <DefaultColumnTermPathValue> columnValues) { using (var clientContext = list.Context as ClientContext) { List <DefaultColumnTermValue> newValues = new List <DefaultColumnTermValue>(); foreach (var value in columnValues) { DefaultColumnTermValue newValue = new DefaultColumnTermValue(); newValue.FieldInternalName = value.FieldInternalName; newValue.FolderRelativePath = value.FolderRelativePath; foreach (var termpath in value.TermPaths) { var term = clientContext.Site.GetTaxonomyItemByPath(termpath) as Term; newValue.Terms.Add(term); } newValues.Add(newValue); } list.SetDefaultColumnValues(newValues); } }
protected override void ExecuteCmdlet() { List list = null; if (List != null) { list = List.GetList(SelectedWeb); } if (list != null) { if (list.BaseTemplate == (int)ListTemplateType.DocumentLibrary || list.BaseTemplate == (int)ListTemplateType.WebPageLibrary || list.BaseTemplate == (int)ListTemplateType.PictureLibrary) { Field field = null; // Get the field if (Field.Field != null) { field = Field.Field; ClientContext.Load(field); ClientContext.ExecuteQueryRetry(); field.EnsureProperties(f => f.TypeAsString, f => f.InternalName); } else if (Field.Id != Guid.Empty) { field = list.Fields.GetById(Field.Id); ClientContext.Load(field, f => f.InternalName, f => f.TypeAsString); ClientContext.ExecuteQueryRetry(); } else if (!string.IsNullOrEmpty(Field.Name)) { field = list.Fields.GetByInternalNameOrTitle(Field.Name); ClientContext.Load(field, f => f.InternalName, f => f.TypeAsString); ClientContext.ExecuteQueryRetry(); } if (field != null) { IDefaultColumnValue defaultColumnValue = null; if (field.TypeAsString == "Text" || field.TypeAsString == "Choice" || field.TypeAsString == "MultiChoice") { var values = string.Join(";", Value); defaultColumnValue = new DefaultColumnTextValue() { FieldInternalName = field.InternalName, FolderRelativePath = Folder, Text = values }; } else { List <Term> terms = new List <Term>(); foreach (var termString in Value) { Guid termGuid; Term term; if (Guid.TryParse(termString, out termGuid)) { var taxSession = ClientContext.Site.GetTaxonomySession(); term = taxSession.GetTerm(termGuid); ClientContext.ExecuteQueryRetry(); } else { term = ClientContext.Site.GetTaxonomyItemByPath(termString) as Term; } if (term != null) { terms.Add(term); } } if (terms.Any()) { defaultColumnValue = new DefaultColumnTermValue() { FieldInternalName = field.InternalName, FolderRelativePath = Folder, }; terms.ForEach(t => ((DefaultColumnTermValue)defaultColumnValue).Terms.Add(t)); } } list.SetDefaultColumnValues(new List <IDefaultColumnValue>() { defaultColumnValue }); } } else { WriteWarning("List is not a document library"); } } }
public void Provision(ClientContext ctx, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation, TokenParser tokenParser, PnPMonitoredScope scope, string configurationData) { if (!string.IsNullOrEmpty(configurationData)) { // Get the current web var web = ctx.Web; // Read configuration data from the template var configuration = XDocument.Parse(configurationData); var ns = configuration.Root.GetDefaultNamespace(); var libraries = configuration.Descendants(ns + "Library"); foreach (var library in libraries) { var libraryTitle = library.Attribute("Title").Value; //Get the library List list = ctx.Web.Lists.GetByTitle(libraryTitle); if (list != null) { var items = library.Descendants(ns + "Default"); foreach (var item in items) { // Get configuration infos var fieldName = item.Attribute("InternalName").Value; var fieldValue = item.Attribute("Value").Value; var folder = item.Attribute("Folder").Value; // Get the field var field = list.Fields.GetByInternalNameOrTitle(fieldName); ctx.Load(field, f => f.InternalName, f => f.TypeAsString); ctx.ExecuteQueryRetry(); if (field != null) { IDefaultColumnValue defaultColumnValue = null; if (field.TypeAsString == "Text") { var values = string.Join(";", fieldValue); defaultColumnValue = new DefaultColumnTextValue() { FieldInternalName = field.InternalName, FolderRelativePath = folder, Text = values }; } else { var terms = new List <Microsoft.SharePoint.Client.Taxonomy.Term>(); var values = fieldValue.Split(';'); foreach (var termString in values) { var term = ctx.Site.GetTaxonomyItemByPath(termString); if (term != null) { terms.Add(term as Microsoft.SharePoint.Client.Taxonomy.Term); } } if (terms.Any()) { defaultColumnValue = new DefaultColumnTermValue() { FieldInternalName = field.InternalName, FolderRelativePath = folder, }; terms.ForEach(t => ((DefaultColumnTermValue)defaultColumnValue).Terms.Add(t)); } } if (defaultColumnValue != null) { list.SetDefaultColumnValues(new List <IDefaultColumnValue>() { defaultColumnValue }); } } } } } } }
protected override void ExecuteCmdlet() { List list = null; if (List != null) { list = SelectedWeb.GetList(List); } if (list != null) { if (list.BaseTemplate == (int)ListTemplateType.DocumentLibrary) { Field field = null; // Get the field if (Field.Field != null) { field = Field.Field; if (!field.IsPropertyAvailable("TypeAsString")) { ClientContext.Load(field, f => f.TypeAsString); } if (!field.IsPropertyAvailable("InternalName")) { ClientContext.Load(field, f => f.InternalName); } ClientContext.Load(field); ClientContext.ExecuteQueryRetry(); } else if (Field.Id != Guid.Empty) { field = list.Fields.GetById(Field.Id); ClientContext.Load(field, f => f.InternalName, f => f.TypeAsString); ClientContext.ExecuteQueryRetry(); } else if (!string.IsNullOrEmpty(Field.Name)) { field = list.Fields.GetByInternalNameOrTitle(Field.Name); ClientContext.Load(field, f => f.InternalName, f => f.TypeAsString); ClientContext.ExecuteQueryRetry(); } if (field != null) { IDefaultColumnValue defaultColumnValue = null; if (field.TypeAsString == "Text") { var values = string.Join(";", Value); defaultColumnValue = new DefaultColumnTextValue() { FieldInternalName = field.InternalName, FolderRelativePath = Folder, Text = values }; } else { List <Term> terms = new List <Term>(); foreach (var termString in Value) { var term = ClientContext.Site.GetTaxonomyItemByPath(termString); if (term != null) { terms.Add(term as Term); } } if (terms.Any()) { defaultColumnValue = new DefaultColumnTermValue() { FieldInternalName = field.InternalName, FolderRelativePath = Folder, }; terms.ForEach(t => ((DefaultColumnTermValue)defaultColumnValue).Terms.Add(t)); } } list.SetDefaultColumnValues(new List <IDefaultColumnValue>() { defaultColumnValue }); } } else { WriteWarning("List is not a document library"); } } }
static void UpdateList(ClientContext ctx, Web web, string listTitle, DefaultFields defaultFields) { List list = web.GetListByTitle(listTitle); FieldCollection fields = list.Fields; ctx.Load(fields, tcol => tcol.Include(t => t.InternalName), tcol => tcol.Where(t => t.TypeAsString == "TaxonomyFieldType" || t.TypeAsString == "TaxonomyFieldTypeMulti")); ctx.ExecuteQuery(); List <IDefaultColumnValue> defaultValues = new List <IDefaultColumnValue>(); var currentDefaultValues = list.GetDefaultColumnValues(); string[] guid; string[] current; foreach (var newDefaultValue in defaultFields) { try { Field found = fields.GetFieldByInternalName(newDefaultValue.InternalName); if (found != null) { bool update = true; if (currentDefaultValues != null) { foreach (var currentDefaultValue in currentDefaultValues) { current = currentDefaultValue.Values.ToArray(); if (newDefaultValue.InternalName == current[1]) { guid = current[2].Split('|'); //{[Value, -1;#Internal|3361fef0-33ac-457d-8a1d-df19735ffcb1]} if (guid.Length > 0) { if (newDefaultValue.OriginGuid != guid[1]) { update = false; } } } } } if (update) { var defaultColumnValue = new DefaultColumnTermValue(); defaultColumnValue.FieldInternalName = newDefaultValue.InternalName; defaultColumnValue.FolderRelativePath = "/"; defaultColumnValue.Terms.Add(newDefaultValue.TermItem); defaultValues.Add(defaultColumnValue); Log.Info(web.Url, list.Title, newDefaultValue.InternalName, ""); } } } catch (Exception ex) { Log.Error(web.Url, list.Title, newDefaultValue.InternalName, ex.Message); } } if (defaultValues.Count > 0) { try { list.SetDefaultColumnValues(defaultValues); } catch (Exception ex) { Log.Error(web.Url, list.Title, "", ex.Message); } } }