0

I've used this guide to create my batch and update the items.

var list = pnp.sp.web.lists.getByTitle("Categories");
list.getListItemEntityTypeFullName().then(entityTypeFullName => {
    var batch = pnp.sp.createBatch();
    $(`.${css.SortContent} table tbody tr`).each(function (ind, con) {
        var id: any = $(con).attr("data-catid");
        var newOrder: any = parseInt($(con).find(`.cat_select :selected`).text());
        list.items.getById(id).inBatch(batch).update({
            Order: newOrder
        }, "*", entityTypeFullName).then(() => {
            console.log("test");
        });
    });
    batch.execute().then(r => {
        console.log(r);
    }).catch((e) => {
        console.log(e);
    });
});

Information: There are 3 tr's inside the body. In my test, they give these results:

id = 1; newOrder = 2
id = 2; newOrder = 1
id = 3; newOrder = 3

When I run the code, this is the following result:

console.log(test) never runs
console.log(r) logs undefined
console.log(e) never runs

If I log batch just before it's executed, I'm able to see 3 items inside. So I know the items are added to the batch.

But the items are never updated, and all I get is.

Error: Error making HttpClient request in queryable: [400] Bad Request

Image of the batch log:

enter image description here

Can anyone tell me what I'm doing wrong?

Update: It looks like "X-HTTP-Method": "MERGE" is missing.

enter image description here

0

No need to get the entityTypeFullName via REST API calls when making updates in a single list.

Similarly, you need not pass the entityTypeFullName parameter while making an update request because the framework will handle it for you.

Modify the code as below and check:

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

var batch = pnp.sp.createBatch();
$(`.${css.SortContent} table tbody tr`).each(function (ind, con) {
    var id: any = $(con).attr("data-catid");
    var newOrder: any = parseInt($(con).find(`.cat_select :selected`).text());
    list.items.getById(id).inBatch(batch).update({
        Order: newOrder
    }).then(d => {
        console.log("test" + d);
    });
});
batch.execute().then(r => {
    console.log(r);
    console.log("batch update successful");
}).catch(e => {
    console.log(e);
});

If you are using it inside SPFx context, make sure to initialize the PnP context as below in your webpart's .ts file before the Render method:

/**
* Initialize the web part.
*/
protected onInit(): Promise<void> {

 return super.onInit().then(_ => {
   pnp.setup({
     spfxContext: this.context
   });
 });
}

FYI - i just tried the below code with hardcoded ID values and it works successfully.

var list = pnp.sp.web.lists.getByTitle('Custom List');

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

list.items.getById(1).inBatch(batch).update({
    Title: "Updated from batch 1"
}).then(d => console.log(d));

list.items.getById(2).inBatch(batch).update({
    Title: "Updated from batch 2"
}).then(d => console.log(d));

list.items.getById(3).inBatch(batch).update({
    Title: "Updated from batch 3"
}).then(d => console.log(d));

batch.execute().then(d => console.log("Batch update success"));
  • Thanks for your answer, I've tried your code and sadly it didnt work for me. still the same error – Peter Hansen Aug 15 '18 at 6:22
  • Gautam, I've updated the post with a new image, It looks like "X-HTTP-Method": "MERGE" is missing, is that correct? – Peter Hansen Aug 15 '18 at 6:48
  • Did you try the 2nd code snippet ? What version of PnP JS are you using ? I used the 3.0.10 js from here and tried it in browser console. It works – Gautam Sheth Aug 15 '18 at 7:54
  • I've used npm to install sp-pnp-js, and i've made sure it's using the latest update- – Peter Hansen Aug 15 '18 at 8:03
  • Are you using it inside spfx webpart ? Also, can you try with hardcoded values of ID and title columns? Have updated the answer to include the spfx init section. – Gautam Sheth Aug 28 '18 at 5:43

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.