Pnpjs: sp.web.getFileByServerRelativeUrl().getItem() is trying to fetch item with wrong ID

Created on 5 Jul 2018  路  5Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [X] Bug
  • [ ] Question
  • [ ] Documentation gap/issue

Version

1.1.1

Expected / Desired Behavior / Question

After removing a folder in a document library, containing a specific file. I want to be able to create the folder again and upload a file with the same name in this folder again, and be able to fetch the item by using the sp.web.getFileByServerRelativeUrl().getItem() method.

Observed Behavior

When uploading a file to the folder it initially works. But when you remove the folder, recreate it again and upload the same file again, an error is thrown:

https://tentant.sharepoint.com/sites/kb-KB20/_api/Web/Lists(guid'd59d2f62-5122-4cec-bc94-e6c473d11508')/Items(267) 404 ()

The item ID that is being used is not the correct one.

Steps to Reproduce

  1. In the standard "Documents" library, create a folder "A1"
    sp.web.getFolderByServerRelativeUrl().folders.add()

  2. Add a file "IT.png" to the folder ussing the addChunked method
    folder.files.addChunked()

  3. Retrieve the related item with the help of getFileByServerRelativeUrl method. Consider that I'm not using the file.getItem() method because of an issue that I registered here getItem() after Folder.files.addChunked() gives 404
    _To this point everything works as expected and the item is retrievable_
    sp.web.getFileByServerRelativeUrl().getItem()

  4. Manually select the created folder and delete the whole folder

  5. Repeat steps 1 to 3

  6. When tyring to fetch the item from the uploaded file. A 404 error is thrown while trying to fetch the item with a wrong ID.

    https://tentant.sharepoint.com/sites/kb-KB20/_api/Web/Lists(guid'd59d2f62-5122-4cec-bc94-e6c473d11508')/Items(267) 404 ()

To take into consideration for finding a solution

When I remove the deleted folder also from the recycle bin and I repeat steps 1 to 3 again, it works again. So it seems while trying to fetch the item from URL, it still sees the item that is in the deleted folder in the recycle bin.

If I remember correctly, when removing a file or folder from a document library, these are still kept in the same location but can only be seen in the recycle bin. However the API should be able to handle this.
I'm not sure if this is actually a bug in pnp or in SharePoint itself.

code answered question

All 5 comments

Interesting one. Thanks for the detailed information, Mathieu. Will investigate this.

Connecting the issue with this one.

Was able to reproduce in an object which is returned after a file is uploaded.

Yet, any amount of upload/recycle do not cause the same behavior for web.getFileByServerRelativeUrl, always a correct Id. Maybe that's because for quick tests I use SP Editor which still uses 1.1.0 will do the same research with latest versions.

We have seen this before in exactly this scenario. There is a small amount of caching that is done on the server side and when you are deleting and immediately recreating the folder/file with the same name and path we get back the wrong ID. I am not sure there is a lot we can do on this one.

@EagleWizard28 - I understand you want to do this, but can you expand on why a bit? Perhaps we can come up with an alternative way to approach the problem.

@patrick-rodgers thanks for confirming that there is indeed some caching in play.

As far as the scenario in which this will happen, this will probably mainly happen in the development stage. I'm testing a multi file upload to a folder automatically created, based on page title. For testing, I delete the folder from time to time and my test files stay of course the same.

Indeed not sure if there is a lot to be done about it, since the caching happens server side. Maybe when I'm able to use the uploadResult.file.getItem() directly, I'll have a reference to the item from the uploaded file. But as I stated in issue #159 this is not working.

Going to close this one, but have merged a fix for #159 which should help. When I do testing I just create random file names to ensure I avoid any caching issues. The other option if you really need the file/folder names to be the same is to wait a bit before recreating them. You can test it out but as little as 30 seconds might be enough. Thanks!

Great! Thanks for the feedback @patrick-rodgers

Was this page helpful?
0 / 5 - 0 ratings