如何使用csom代码批量创建sharepoint列表项

时间:2017-10-29 10:13:22

标签: sharepoint sharepoint-2010 sharepoint-2013

任何人都可以告诉我如何使用CSOM代码批量创建多个SharePoint列表项。

我指的是使用CSOM代码创建批处理,类似于下面的代码。下面的代码在服务器对象模型中我希望在CSOM中实现相同的功能。

region [方法:批量添加列表项目详细信息]

    public void AddCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
    {
        StringBuilder sbInsertCanteenMenu = new StringBuilder();
        sbInsertCanteenMenu.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
                bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;
                web.Update();
                foreach (CanteenMenuDetails item in objCanteenMenuDetails)
                {
                    sbInsertCanteenMenu.AppendFormat("<Method ID=\"{0}\">" +
                                            "<SetList>{1}</SetList>" +
                                            "<SetVar Name=\"ID\">New</SetVar>" +
                                            "<SetVar Name=\"Cmd\">Save</SetVar>" +
                                            "<SetVar Name=\"{3}Title\">{2}</SetVar>" +
                                            "<SetVar Name=\"{3}Location\">{4}</SetVar>" +
                                            "<SetVar Name=\"{3}WeekDays\">{5}</SetVar>" +
                     "</Method>", item.Title, lstCanteenMenu.ID, item.Title, "urn:schemas-microsoft-com:office:office#", item.LocationID, item.WeekDays);
                }
                sbInsertCanteenMenu.Append("</Batch>");
                web.ProcessBatchData(sbInsertCanteenMenu.ToString());
                web.AllowUnsafeUpdates = allowUnsafeUpdates;
                web.Update();
            }
        }
    }
    #endregion

1 个答案:

答案 0 :(得分:2)

您可以使用CSOM C#:

尝试以下操作
var list = context.Web.Lists.GetByTitle("CustomList");

for (var i = 0; i < 100; i++)
{
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem oListItem = list.AddItem(itemCreateInfo);

    oListItem["Title"] = "My New Item " + i.ToString();
    oListItem["Description"] = "Test description";
    oListItem.Update();
}                


context.ExecuteQuery();

如果您想使用JSOM(javascript代码),可以按如下方式使用它:

var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('CustomList');   

for(var i = 0; i< 100; i++){

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);  
    oListItem.set_item('Title', 'New Item ' + i);  
    oListItem.set_item('Description', 'Test desc');  
    oListItem.update();
    itemArray[i] = oListItem;
    clientContext.load(itemArray[i]);
}

clientContext.executeQueryAsync(function(){ 
    console.log("success");
}, function(){
    console.log("error");
});

请根据您的列表名称和内部列名称进行更改。

参考 - Batch operations using javascript

更新 - 根据评论修改

foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{ 

    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem oListItem = list.AddItem(itemCreateInfo);

    oListItem["Title"] = item.Title;
    oListItem["Location"] = item.LocationID;
    oListItem["WeekDays"] = item.WeekDays;  
    oListItem.Update();

}
context.ExecuteQuery();