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

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+""
                  });
              });
          });
| improve this answer | |
  • This is my updated code jsfiddle.net/Pepozzo/co69z5e2/1 and still returning same error. – Pepozzo Jul 16 '19 at 10:31
  • can you try with approach 2 ? I have added no-cache headers – Gautam Sheth Jul 16 '19 at 11:00
  • This second approach returns "ODataDefaultParser is not defined" – Pepozzo Jul 16 '19 at 11:07

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.