1

我一直在阅读有关在 SP 列表中排队多个项目以进行处理后仅执行一个查询的信息。

我有以下代码,其中有一个按钮,可以将一堆记录加载到 Excel 电子表格中的空 SP 列表中。如果我在每个 item.update 之后执行executequery,它就可以工作......但我认为应该有更好的方法......我可以将数组中的项目排队并一次发送所有项目。我看到了有关如何使用现有列表项执行此操作的示例……但我还没有看到使用新项执行此操作的方法。我能够通过删除项目来做到这一点......将它们全部缓存起来并执行一个执行查询以删除所有项目......这显然比 eq 快 1000 倍。因此,希望通过添加新项目来做同样的事情。感谢您的任何想法。

急诊室

private void buttonAddIndividualApplicants_Click(object sender, EventArgs e)
        {
            logThis("Start adding all individual applicants...");
            //Set up SCOM 
            ClientContext context = new ClientContext(textBoxSPSite.Text);
            List list = context.Web.Lists.GetByTitle(textBoxSPList.Text);
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = list.AddItem(itemCreateInfo);

            //ListItemCollection items = new[];

            //Set up Excel
            var package = new ExcelPackage(new FileInfo(GlobalVars.ssFileName));
            ExcelWorksheet workSheet = package.Workbook.Worksheets[GlobalVars.ssApplicantsTab];

            //Start iterating through ss
            for (int i = workSheet.Dimension.Start.Row + 1;
                     i <= workSheet.Dimension.End.Row;
                     i++)
            {
                logThis("Row:" + i);
                string van = workSheet.Cells[i, 1].Value.ToString();
                string appID = workSheet.Cells[i, 2].Value.ToString();
                string name = workSheet.Cells[i, 3].Value.ToString();
                string email = workSheet.Cells[i, 4].Value.ToString();

                logThis(van + "-" + appID + "-" + name + "-" + email + " queued for processing.");

                //Push an item to the stack:
                oListItem["AppID"] = appID;
                oListItem["ApplicantName"] = name;
                oListItem["VAN"] = van;
                oListItem["ApplicantEmailAddress"] = email;
                oListItem.Update();
                //context.ExecuteQuery();  ***with ExecuteQuery here it works

            }
            //After all items pushed onto stack...call ExQuery to apply            
            logThis("Starting ExecuteQuery to process queued list items...");
            context.ExecuteQuery();   //Here it gives me only last name in spreadsheet         
            logThis("FINISHED ADDING INDIVIDUAL APPLICANTS");
        }
4

1 回答 1

2

想通了。我的 ListItem 放错了地方。在我的 for 循环中需要它,而不是在它之外。下面的工作代码:

private void buttonAddIndividualApplicants_Click(object sender, EventArgs e)
        {
            logThis("Start adding all individual applicants...");
            //Set up SCOM 
            ClientContext context = new ClientContext(textBoxSPSite.Text);
            List list = context.Web.Lists.GetByTitle(textBoxSPList.Text);
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

            //Set up Excel
            var package = new ExcelPackage(new FileInfo(GlobalVars.ssFileName));
            ExcelWorksheet workSheet = package.Workbook.Worksheets[GlobalVars.ssApplicantsTab];

            //Start iterating through ss
            for (int i = workSheet.Dimension.Start.Row + 1;
                     i <= workSheet.Dimension.End.Row;
                     i++)
            {
                ListItem oListItem = list.AddItem(itemCreateInfo);  //**MOVED ListItem into for loop fixed it
                logThis("Row:" + i);
                string van = workSheet.Cells[i, 1].Value.ToString();
                string appID = workSheet.Cells[i, 2].Value.ToString();
                string name = workSheet.Cells[i, 3].Value.ToString();
                string email = workSheet.Cells[i, 4].Value.ToString();

                logThis(van + "-" + appID + "-" + name + "-" + email + " queued for processing.");

                //Push an item to the stack:
                oListItem["AppID"] = appID;
                oListItem["ApplicantName"] = name;
                oListItem["VAN"] = van;
                oListItem["ApplicantEmailAddress"] = email;
                oListItem.Update();
                //context.ExecuteQuery();
            }
            //After all items pushed onto stack...call ExQuery to apply            
            logThis("Starting ExecuteQuery to process queued list items...");
            context.ExecuteQuery();
            logThis("FINISHED ADDING INDIVIDUAL APPLICANTS");
        }
于 2017-12-21T02:14:34.867 回答