1

I'm trying to copy a file within a document library (SharePoint online) via the Graph SDK. However the document library have a custom unique field.

enforce unique values

The file is copied to the same folder.

await client
    .Drives[driveId]
    .Items[sourceItemId]
    .Copy(destinationFilename)
    .Request()
    .PostAsync();

The above throws this error: One or more fields with unique constraints already has the provided value

How do I copy a file into the same folder when there is a unique field constraint?

Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • Are you trying to use the same file name as the file you are copying?? – Ryan Wilson Sep 11 '20 at 12:48
  • @RyanWilson no, it's a completely new GUID name. And the document library only have like 5 files (for now) so I'm confident this isn't a filename collision – Snæbjørn Sep 11 '20 at 16:46
  • Is there any other filed that uniquely identify your file? – Shiva Keshav Varma Sep 14 '20 at 16:14
  • Sure, the filename and sharepoint have some internal stuff. But it's not relevant. When I remove the unique constraint from my custom field then `copy` works. So it's the unique custom field that's the culprit. – Snæbjørn Sep 15 '20 at 07:26
  • @Snæbjørn the question is unclear - you explicitly specified that this field should be unique. If you try to duplicate the file, you'd also get a duplicate field. The error is expected. If you don't want this, *don't* just duplicate the file, change it's metadata when creating it – Panagiotis Kanavos Sep 15 '20 at 07:33
  • @Snæbjørn btw what you work with is SharePoint, not the Graph API. That's just putting lipstick on a .... you get it. It's still SharePoint. The chained methods may look fashionable but they hide what's really going on. There are no drives, a document library is neither a file system nor a database. SharePoint has site collections, sites, document libraries and lists. When you create a document, you *can* specify the metadata you want – Panagiotis Kanavos Sep 15 '20 at 07:39
  • @PanagiotisKanavos so SharePoint doesn't have a way to alter metadata/fields when copying? It alters the filename, so that at least works. I have a hard time believing they implemented unique fields with such a shortsighted implementation. – Snæbjørn Sep 15 '20 at 08:27
  • @Snæbjørn quite the opposite - SharePoint *does* have this, even though the Graph API may not expose it. You'll have to use SharePoint's APIs to do this, eg CSOM. In the past I used server-side code for on-prem SharePoint to generate documents based on a "template" document, folder or site. – Panagiotis Kanavos Sep 15 '20 at 09:17
  • @Snæbjørn as for `a shortsighted implementation` you have no idea. There's a real reason SP is one of the most hated technologies, and it's not lack of familiarity. The priority was always to release new features on a schedule, not stabilize the product, so *all* versions came with unfinished features and serious bugs. The *development* team never worked on those bugs, only new features. Fixing bugs was the job of a separate support team. And MS Marketing sold it as a solution to everything, whether it worked or not, letting partners deal with irate customers and unrealistic expectations – Panagiotis Kanavos Sep 15 '20 at 09:25
  • Thanks I'll have a look at the SharePoint API. And oh my.. I'm glad I'm only doing minor integrations to sharepoint :/ – Snæbjørn Sep 15 '20 at 15:47

0 Answers0