0

I have a dynamically generated array with IDs of different items and different lists.

What I want:

Return custom fields of every item in my array.

The Problem:

No Problem until an item is deleted. Then I'll get the following error:

Item does not exist. It may have been deleted by another user.

My idea:

Maybe there is an easy option to check if an item exists or not. The array with the item IDs can become large, with more than 200 items of different lists. So I think it's not a good idea to make an Ajax-Request for every single item. Isn't there a better solution?

My Code:

        var context = SP.ClientContext.get_current();                        
        for(var i in items){
            var web = context.get_web(items[i]['webId']);
            var list = web.get_lists().getById(items[i]['listId']);
            var item = list.getItemById(items[i]['itemId']);

            for(var j in columns){
                if(columns[j].getToLoad('web').length > 0){
                    for(var k in columns[j].getToLoad('web')){
                        context.load(web, columns[j].getToLoad('web')[k]);
                    }
                }
                if(columns[j].getToLoad('list').length > 0){
                    for(var k in columns[j].getToLoad('list')){
                        context.load(list, columns[j].getToLoad('list')[k]);
                    }
                }
                if(columns[j].getToLoad('item').length > 0){
                    for(var k in columns[j].getToLoad('item')){
                        context.load(item, columns[j].getToLoad('item')[k]);
                    }
                }
            }                
        }


        context.executeQueryAsync(
            buildTable.bind(this, items),
            function(a, b){
                console.error('ERROR: ' + b.get_message());
            }
        );
  • The very best solution for me would be an option to simply ignore non-existing items. – Jan Worpenberg Aug 12 '19 at 08:31
  • How are you using this script? I understand you are building something based off the lists...but what are you doing after this with the information? My main confusion is how does your code know to run again or that an item was deleted after this code runs the first time? – Matt Aug 12 '19 at 16:33
  • In short I build my own Table in Sharepoint that contains items of different lists. I have an array, that contains the web-ID, the list-ID and the item-ID. I load this array from the localstorage. First I load webs and the needed fields connected to the webs. Second I load lists and the needed fields connected to the lists. Last I load the items and the needed fields connected to the items. Finally I do the executeQueryAsync to perform the query. And it really works like a charm - until there is an item deleted from sharepoint, BUT NOT from my array in my localstorage. – Jan Worpenberg Aug 13 '19 at 07:04
  • The error does NOT appear in the buildTable function. It appears in the line: console.error('ERROR: ' + b.get_message()); – Jan Worpenberg Aug 13 '19 at 07:07

1 Answers1

0

Instead of use the JSOM, we can use batch requests with the REST API to achieve your requirement.If any of the child operations fails, the others still complete and are not rolled back.

Example Code: how to execute SharePoint REST Batch request

var jsonSPHeaders = {  
    "Accept": "application/json;odata=verbose", 
    "Content-Type": "application/json;odata=verbose",
    "DataServiceVersion": "3.0" 
};

OData.request( {
    requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/$batch",
    method: "POST",
    headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(),
               "DataServiceVersion": "3.0" },
    data: { __batchRequests: [
       { requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Pages')/items", method: "GET" , headers: jsonSPHeaders },
       { requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Tasks')/items", method: "GET", headers: jsonSPHeaders }
    ]}
},
function (data, response) {
    console.log('Pages list:');
    printListItems(data.__batchResponses[0].data);
    console.log('Tasks list:');
    printListItems(data.__batchResponses[1].data);
}, 
null, 
OData.batchHandler);



function printListItems(data){
   data.results.forEach(function(item){
       console.log(item.Title); 
   });
}
LZ_MSFT
  • 4,079
  • 1
  • 8
  • 9