pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
742 stars 302 forks source link

attachmentFiles.getByName does not encode the filename #2931

Closed nathan-prijot closed 3 months ago

nathan-prijot commented 5 months ago

Major Version

3.x

Minor Version Number

3.22.0

Target environment

SharePoint Framework

Additional environment details

Typical SPFx v1.18.2

Expected or Desired Behavior

If I have an attachments literally called Lorem%20ipsum.docx the following should work:

const sp = spfi(...)
sp.web.lists.getByTitle("My List").items.getById(1).attachmentFiles.getByName("Lorem%20ipsum.docx").recycle();
// Should calls https://myorg.sharepoint.com/sites/MySite/_api/web/lists/getByTitle('My List')/items(1)/attachmentFiles('Lorem%2520ipsum.docx')/recycleObject 

Observed Behavior

It does not encode the filename and calls https://myorg.sharepoint.com/sites/MySite/_api/web/lists/getByTitle('My List')/items(1)/attachmentFiles('Lorem%20ipsum.docx')/recycleObject. The filename is not recognized which generate a 400 error.

Since attachmentFiles.add nows encode the filename, this seems a bit odd to work with.

Note that SharePoint itself seems confused about it because the ServerRelativeUrl returned by the SharePoint API will also be invalid (as well as the odata.id and odata.editLink).

Steps to Reproduce

  1. Use the attachmentFiles.add with as a filename Lorem%20ipsum.docx.
  2. Use the attachmentFiles.getByName with the same filename.
nathan-prijot commented 5 months ago

Additionally, attachmentFiles.getByName does not double the ' in the file name while attachmentFiles.add does. It's quite funny because in v2, if I'm not mistaken, you had to encode the filename and double the ' for attachmentFiles.add and not for attachmentFiles.getByName. So it's the opposite here in v3.

nathan-prijot commented 5 months ago

For those wondering, here is what you need to do if you want getByName to work with the 'raw' file name:

const escapedFileName = encodeURIComponent(attachmentName.replace(/'/g, "''"));
attachmentFiles.getByName(escapedFileName).recycle();

Reminder that you have to do it for attachmentFiles.getByName but not for attachmentFiles.add.

patrick-rodgers commented 3 months ago

Fixed in v4, won't fix in v3.

patrick-rodgers commented 3 months ago

Closing with merge of fix for v4

github-actions[bot] commented 3 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.