예제 #1
0
        public void ClearAllDefaultColumnValuesTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                // arrange
                List <IDefaultColumnValue> defaultValues = new List <IDefaultColumnValue>();

                var testFooDefaultValue = new DefaultColumnTextValue();
                testFooDefaultValue.Text = "Foo";
                testFooDefaultValue.FieldInternalName  = _textFieldName;
                testFooDefaultValue.FolderRelativePath = "/"; // Root folder

                var testBarDefaultValue = new DefaultColumnTextValue();
                testBarDefaultValue.Text = "Bar";
                testBarDefaultValue.FieldInternalName  = _textFieldName2;
                testBarDefaultValue.FolderRelativePath = "/"; // Root folder

                defaultValues.Add(testFooDefaultValue);
                defaultValues.Add(testBarDefaultValue);

                var list = clientContext.Web.Lists.GetById(_listId);

                list.SetDefaultColumnValues(defaultValues);

                // act
                list.ClearDefaultColumnValues();
                var actual = list.GetDefaultColumnValues();

                // assert
                Assert.IsNull(actual);
            }
        }
예제 #2
0
        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 void OverwriteDefaultColumnValuesTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                // arrange
                List <IDefaultColumnValue> defaultValues = new List <IDefaultColumnValue>();

                var testFooDefaultValue = new DefaultColumnTextValue
                {
                    Text = "Foo",
                    FieldInternalName  = _textFieldName,
                    FolderRelativePath = "/" // Root folder
                };

                var testBarDefaultValue = new DefaultColumnTextValue
                {
                    Text = "Bar",
                    FieldInternalName  = _textFieldName2,
                    FolderRelativePath = "/" // Root folder
                };

                defaultValues.Add(testFooDefaultValue);
                defaultValues.Add(testBarDefaultValue);

                var list = clientContext.Web.Lists.GetById(_listId);

                list.SetDefaultColumnValues(defaultValues);
                var result       = list.GetDefaultColumnValues();
                var itemToRemove = result.First(d => d["Field"] == _textFieldName);
                result.Remove(itemToRemove);
                var expected = result;

                // act
                list.SetDefaultColumnValues(new List <IDefaultColumnValue> {
                    testBarDefaultValue
                }, true);
                var actual = list.GetDefaultColumnValues();

                // assert
                CollectionAssert.AreEqual(actual[0], expected[0]);
            }
        }
예제 #4
0
        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");
                }
            }
        }
예제 #5
0
        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
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #6
0
        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");
                }
            }
        }