3

I have a SharePoint List in which I would like to update every relevant item's column value use REST API

I can update the information for each individual item given its ID, but I can not seem to figure out how to use REST to update them all.

Here is the code I have so far. If you know what I'm doing wrong or have any experience with this please let me know.

function Name() {
$.ajax({
    url: window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl + "/" + "_api/Web/Lists/getbytitle('ListName')/items?$filter=Name eq 'Name'",
    method: "POST",
    data: JSON.stringify({__metadata:{type: "SP.Data.ListNameListItem"},Column: "Value"}),
    headers:{   "Accept": "application/json; odata=verbose",
        "Content-Type": "application/json;odata=verbose", 
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "X-HTTP-Method":"MERGE",
        "If-Match":"*"
    },
    success: function (data) {console.log(data)},
    error: function (error) {alert(JSON.stringify(error));}
});}
  • No need to put filter in url. first get all items and then put recursive call for item update. – Akshay Oct 17 '17 at 4:28
  • are you on SP 2013 or SP Online ? – Gautam Sheth Oct 17 '17 at 6:16
  • @Akshay I don't want to change every item in the list, just the ones with a specific column value, but recursion is a good idea. I'll try it, thanks – JacobCaldwell Oct 17 '17 at 13:37
4

Since this is SharePoint online, you can make use of the PnP JS core library. It contains a fluent API for working with SharePoint REST API as well as utility and helper functions.

To use that, simply add the pnp.js file's reference in your script editor/content editor/SP hosted app etc.

Once that is done, you can use the batch API to update multiple list items in a single call. This is quite fast but will work only in SPO.

The code for that is as below, do modify as per your requirements:

var list = $pnp.sp.web.lists.getByTitle("ListName");

list.getListItemEntityTypeFullName().then(entityTypeFullName => {

    var items = list.items;

    items.filter("Name eq 'Name'").get().then(function(listItems) {

        var batch = $pnp.sp.createBatch();

        listItems.forEach(function(item, index) {

            items.getById(item.Id).inBatch(batch).update({
                Column: "Value",
            },"*", entityTypeFullName).catch(console.log);

        })

        batch.execute().then(console.log).catch(console.log)
    });

});

Secondly, you can also use the old school foreach loop which will work in both OnPrem and Online as below:

$pnp.sp.web.lists.getByTitle("ListName").items.filter("Name eq 'Name'").get().then((items) => {

    if (items.length > 0) { 

        items.forEach(function(item, index) {

            $pnp.sp.web.lists.getByTitle("ListName").items.getById(item.Id).update({
                Column: "Value",
            }).then(result => {             
                console.log(JSON.stringify(result));
            });

        });
    }else{
        console.log("no items found");
    }   
}); 

Install & Use - Getting started with PnP JS

Reference - PnP JS - working with list items

  • Oh wow. Thanks for the information. I will defiantly have to look into this! – JacobCaldwell Oct 17 '17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.