简体   繁体   中英

Get mulitple values in a library column for each list items in Sharepoint with CSOM

I have a very simple document library in SharePoint Online :

mysite.sharepoint.com/site/Documents

I added a library column on the library and it is a multiple choice allowed.

The documents have multiple subjects :

Document1.docx : Maths, Science, Internet
Document2.docx : Maths, Other, Science

I want to list each item recursivly in the console with the values on that colum and replace one of the value.

I want to replace "Maths" subject with "Mathematics" with some C# code and the CSOM.

Here is my code. It works when there is only one value, but when there is multiplle subjects selected for a document, it seems to be empty.

I tried to cast but it seems to return an array of objects and its not enumerable.

        ClientContext context = newClientContext("https://mysite.sharepoint.com/site/");

        Web web = context.Web;

        List list = context.Web.Lists.GetByTitle("Documents");

        CamlQuery query = new CamlQuery();
        query.ViewXml = @"<View Scope='Recursive' />";
        ListItemCollection items = list.GetItems(query);

        context.Load(list);
        context.Load(items);

        context.ExecuteQuery();

        foreach (ListItem item in items)
        {

           if ( item["Subjects"] != null && ((item["Subjects"]).ToString()).Contains("Maths")){
                Console.WriteLine(item.Id + " - " + item["Subjects"]);
                item["Subjects"] = "Mathematics";
                item.Update();
                context.ExecuteQuery();
            }
        }

The result should be :

Document1.docx : Mathematics, Science, Internet
Document2.docx : Mathematics, Other, Science

I managed to loop the subjects by casting it in a array of strings :

                var subjects= (string[])item["Subjects"];
                foreach (var subject in subjects)
                {
                    Console.WriteLine(subject);
                }

After I can build an array and update the value :

            string[] newValues = { "Mathematics", "123" };
            item["Subjects"] =  newValues ;

You can add values to the existing choice field column of the SharePoint list from ItemAdded event receiver of the same list. Her you go !!!!!

string newPartitionName = Convert.ToString(properties.ListItem["Title"]); // current item                   
                    using (SPSite site = new SPSite(RBSSiteURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPSecurity.RunWithElevatedPrivileges(delegate
                            {
                                web.AllowUnsafeUpdates = true;
                                SPList spList = web.Lists["Room List"];
                                SPFieldMultiChoice spChoiceField = (SPFieldMultiChoice)spList.Fields["Select Partitions"];
                                spChoiceField.AddChoice(newPartitionName);
                                spChoiceField.Update();
                                spList.Update();
                                web.AllowUnsafeUpdates = false;
                            });
                        }
                    }

Mark as a answer if you find the solution for your project. HAPPY SHAREPOINTING.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM