Pnpjs: File upload to library with 'Enforce unique values' field not working

Created on 30 Mar 2019  路  4Comments  路  Source: pnp/pnpjs

Category

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

Version

Please specify what version of the library you are using: [ 3.0.10 ]

Please specify what version(s) of SharePoint you are targeting: [ Online ]

I'm using the example in the documentation in order to upload a file to a document library and it works fine. But whenever I apply the 'Enforce Unique Values' to a field with default value (a choice field with default value set to 'Choice 1)', and if a file with that field is already set to 'Choice 1', it doesn't work. And it shouldn't. The upload stops working.

The problem is that I tried the operation directly on both classic and modern UI. The classic UI prevents from uploading the file and asks to change the choice field before. But the modern UI completes the upload and applies the 'Choice 1' to that field value, regardless of the 'Unique values' setting.

Even if I do a check beforehand, I still won't be able to complete the operation. Item update can only occur after the code below.

Here is my code:

pnp.sp.web.getFolderByServerRelativeUrl("/sites/spfx/subsiteone/SPFXDocuments").files.add(file.name, file, true)
.then((f: any) => {
});

Thanks for your help.

non-library question

All 4 comments

I changed my comment category to question. I know that this is not a bug and certainly not related to pnp. But my question is and I hope that somebody can give guidance, how do we handle the upload on a case like this ?

The correct way would be to prompt the user to update the unique field before the upload occurs to avoid duplicate values, as the classic UI does. But even if I try do that, I will be able to update that field only after the upload. Which in this case won't work.

The way that the modern UI handle this, by uploading the file with an existing value is clearly not correct.

I could of course deactivate the unique values setting but in this case, this is something that is required for my project.

Thanks

Interesting question...

I'd suggest handling and blocking upload if it breaks business logic rules and it's possible to catch the case via API model.

If I were developing a feature you described, I'd thought on handling upload restriction and action blocking, probably upload to a separate temporary library for the only purpose to "fix" metadata and move the document to permanent storage if metadata is resolved. But I actually do not like it and probably would search for another solution. IMO, field level uniqueness restriction in document libraries is something which I would try to avoid in general.

Thank you @koltyakov for your answer. I totally agree with you on the fact that field level uniqueness restriction should be avoided. But unfortunately, as developer I don't make the requirements.

I guess the only solution left, as you proposed, is simply blocking the upload. Because moving the file(s) to a temporary library would make things too complicated and certainly confuse the user.

I'm just wondering how SharePoint itself handles this internally. I guess it would be nice to at least be able to upload and update the metadata at the same time. This would solve this problem.

Thanks again for you help.

Going to close this as answered. I did noticed that the version number you supplied indicates you are using sp-pnp-js, which is now deprecated and recommend you update to the new libraries as soon as possible. Thanks!

Was this page helpful?
0 / 5 - 0 ratings