简体   繁体   English

使用CSOM在库列中获取Sharepoint中每个列表项的多个值

[英]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 : 我在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. 我想用一些C#代码和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. 您可以从同一列表的ItemAdded事件接收器将值添加到SharePoint列表的现有选择字段列。 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..... 祝大家开心.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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