0

I'm running a small code to upload file in a SharePoint 2016 document library and to change a metadata.

Sometimes (but really often) my code returns error "Error making HttpClient request in queryable [404] Not Found" with the message "Item does not exist. It may have been deleted by another user."

This is my code:

pnp.sp.web.getFolderByServerRelativeUrl("/sites/mysite/ConfigDL").files.add(fileOgg.name, fileOgg, true).then(function (data) {
    var RelativeUrls="/sites/mysite/ConfigDL/"+fileOgg.name;
    //Retrive Document which is uploaded. Start
    pnp.sp.web.getFolderByServerRelativeUrl(RelativeUrls).getItem().then(item => {
        //update start Below Call to Doc List will update the Row baseed on Item.ID
        pnp.sp.web.lists.getByTitle("ConfigDL").items.getById(item.ID).update({
            Number: ""+number+""
        }).then(r => {
            alert(file.name + " upload successfully!");
        });//update end
    }); //Retrive Doc Info End
}); //Upload Document End

I can't get why this error happens randomly and what's wrong with my code. Any suggestion?

1 Answer 1

1

That is mostly due the async nature of the HTTP requests.

To upload the file and then set it properties correctly, you can do so as below:

pnp.sp.web.getFolderByServerRelativeUrl("/sites/mysite/ConfigDL").files.add(fileOgg.name, fileOgg, true).then(f => {

    f.file.getItem().then(item => {
        //update start Below Call to Doc List will update the Row baseed on Item.ID
        item.update({
            Number: ""+number+""
        }).then(r => {
            alert(fileOgg.name + " upload successfully!");
        });//update end
    }); //Retrive Doc Info End
}); //Upload Document End

You can refer the documentation here

Approach 2:

pnp.sp.web.getFolderByServerRelativeUrl("/sites/mysite/ConfigDL").files.add(fileOgg.name, fileOgg, true).then(f => {

      f.file.listItemAllFields.get(new ODataDefaultParser(), {
                  headers: {
                      "Cache-Control": "no-cache",
                  }
              }).then(function (item) {
                  item.update({
                    Number: ""+number+""
                  });
              });
          });
3
  • This is my updated code jsfiddle.net/Pepozzo/co69z5e2/1 and still returning same error.
    – Pepozzo
    Commented Jul 16, 2019 at 10:31
  • can you try with approach 2 ? I have added no-cache headers Commented Jul 16, 2019 at 11:00
  • This second approach returns "ODataDefaultParser is not defined"
    – Pepozzo
    Commented Jul 16, 2019 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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