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