如何使用JSOM在SharePoint列表中插入5000多个项目?

如何使用JSOM在SharePoint列表中插入5000多个项目?,sharepoint,sharepoint-2013,sharepoint-list,sharepoint-jsom,Sharepoint,Sharepoint 2013,Sharepoint List,Sharepoint Jsom,我正在尝试使用JSOM在SharePoint列表中添加5000项。但不幸的是没有得到任何运气 function createListItem() { var clientContext = new SP.ClientContext.get_current(); var oList = clientContext.get_web().get_lists().getByTitle('DummyList'); var itemCreateInfo = new SP.ListI

我正在尝试使用JSOM在SharePoint列表中添加5000项。但不幸的是没有得到任何运气

function createListItem() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('DummyList');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    for (var i = 0; i < 5000; i++) {
        this.oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('ItemNumber', i);
        oListItem.update();
    }

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    console.log('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
函数createListItem(){
var clientContext=new SP.clientContext.get_current();
var oList=clientContext.get_web().get_list().getByTitle('DummyList');
var itemCreateInfo=new SP.ListItemCreationInformation();
对于(变量i=0;i<5000;i++){
this.oListItem=oList.addItem(itemCreateInfo);
oListItem.set_项目('ItemNumber',i);
update();
}
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onQuerySucceeded(){
log('Item created:'+oListItem.get_id());
}
函数onQueryFailed(发送方,参数){
console.log('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}

但一段时间后,服务器停止响应。我知道有一种叫做阈值限制的东西。但根据要求,一次只能储存5000多件物品。我不知道我在哪里犯了错误。请帮忙。

这里有一些建议。它对我的环境起作用。但这需要一些时间(大约20秒),因为所有请求都由服务器排队。您可能可以利用批处理限制来优化请求数量

function createListItem() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('DummyList');

    var items = [];
    var batchLimit = 100;

    for (var i = 0; i < 5000; i++) {
        var itemCreateInfo = new SP.ListItemCreationInformation();
        var newItem = oList.addItem(itemCreateInfo);

        newItem.set_item('ItemNumber', i);
        newItem.update();
        items[i] = newItem;
        clientContext.load(items[i]);

        if (i % batchLimit == 0) {
            console.log("sending batch" + i / batchLimit);                                
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            items = [];
        }            
    }
}

function onQuerySucceeded() {
    console.log('Item created');
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
函数createListItem(){
var clientContext=new SP.clientContext.get_current();
var oList=clientContext.get_web().get_list().getByTitle('DummyList');
var项目=[];
var batchLimit=100;
对于(变量i=0;i<5000;i++){
var itemCreateInfo=new SP.ListItemCreationInformation();
var newItem=oList.addItem(itemCreateInfo);
newItem.set_item('ItemNumber',i);
newItem.update();
项目[i]=新项目;
clientContext.load(items[i]);
如果(i%batchLimit==0){
console.log(“发送批次”+i/batchLimit);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
项目=[];
}            
}
}
函数onQuerySucceeded(){
console.log('Item created');
}
函数onQueryFailed(发送方,参数){
console.log('请求失败。'+args.get_message()+'\n'+args.get_stackTrace());
}

不知怎的,我找到了解决这个问题的办法。我没有使用回调方法,而是像这样使用RESTAPI

function RestAdd()
{

      for(var i = 0 ; i < 5000; i++)
      {
$.ajax  
    ({  
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DummyListForMultiAdd')/items",  
    type: "POST",
    async: false,
    data: JSON.stringify  
    ({  
        __metadata:  
        {  
            type: "SP.Data.DummyListForMultiAddListItem"  
        },  
        ItemNumber: i
    }),  
    headers:  
    {  
        "Accept": "application/json;odata=verbose",  
        "Content-Type": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "X-HTTP-Method": "POST"  
    },  
    success: function(data, status, xhr)  
    { 
    console.log("success: "+i);
    },  
    error: function(xhr, status, error)  
    {  
        console.log("failed: "+i);
    }  
});
}
}
函数RestAdd()
{
对于(变量i=0;i<5000;i++)
{
$.ajax
({  
url:_spPageContextInfo.webAbsoluteUrl+“/_api/web/lists/getbytitle('DummyListForMultiAdd')/items”,
类型:“POST”,
async:false,
数据:JSON.stringify
({  
__元数据:
{  
类型:“SP.Data.DummyListForMultiAddListItem”
},  
项目编号:i
}),  
标题:
{  
“接受”:“application/json;odata=verbose”,
“内容类型”:“应用程序/json;odata=verbose”,
“X-RequestDigest”:$(“#u-RequestDigest”).val(),
“X-HTTP-Method”:“POST”
},  
成功:功能(数据、状态、xhr)
{ 
console.log(“成功:+i”);
},  
错误:函数(xhr、状态、错误)
{  
console.log(“失败:+i”);
}  
});
}
}

我所做的是,我只是将RESTAPI与
async:false
一起使用。它以同步方式添加列表项。在JSOM中,它是异步工作的。

下面是这两种技术的组合

异步函数createListItem(){ 常量url=`${spPageContextInfo.webAbsoluteUrl}/\u api/web/lists/getbytitle('DummyListForMultiAdd')/items`; 对于(让索引=0;索引{ d、 \元数据={ 类型:“SP.Data.DummyListForMultiAddListItem” }; d、 ItemNumber=i+索引; 返回xhr(“post”,url,d); }) 等待承诺。所有(承诺); } 函数xhr(类型、url、数据){ const prom=新承诺((res,rej)=>{ const xhr=new XMLHttpRequest(); xhr.onreadystatechange=()=>{ if(xhr.readyState==XMLHttpRequest.DONE){ 试一试{ 如果(xhr.status==200 | | xhr.status==201){ res(xhr.responseText); }否则{ 常数{ 地位 }=xhr; const name=“XHRError”; 常量消息= xhr.responseText|| “发送或接收请求时出错”; 扔{ 地位 名称 消息 }; } }捕获(错误){ 如果(错误状态){ 错误; }否则{ 雷吉({ 现状:418, name:error.name, 消息:error.message }); } } } }; xhr.open(类型、url); [{ 键:“接受”, 值:“application/json;odata=verbose” }, { 关键字:“内容类型”, 值:“application/json;odata=verbose” }, { 密钥:“X-RequestDigest”, 值:document.querySelector(“#u请求摘要”).value } ].forEach(h=>xhr.setRequestHeader(h.key,h.value)); send(JSON.stringify(data)); }); 返回prom; } }
createListItem()看看这个创建索引可能会带来什么help@GuruparanGiritharan这是用来取回的。我想插入。我知道尝试使用索引插入5000条以上的记录我不熟悉这种方法。你能举个例子吗?谢谢你的建议