0

I already implemented the creation of a document set at library root level. For this I used the following link: Is it possible to create a project documentset using graph API?

I follow the following steps :

1- Retrieve the document library's Drive Id:

GET https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}?$expand=drive

2- Create the folder:

POST https://graph.microsoft.com/v1.0/drives/${library.drive.id}/root/children

The body of the request is the following

{
  "name": ${folderName},
  "folder": {},
}

3- Get the folder's SharePoint item id:

GET https://graph.microsoft.com/v1.0/sites/${siteId}/drives/${library.drive.id}/items/${folder.id}?expand=sharepointids 

4- Update the item in the Document Library so that it updates to the desired Document Set:

PATCH https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}/items/${sharepointIds.listItemId}

I do send the following body to the patch request:

{
  "contentType": {
    "id": "content-type-id-of-the-document-set"
  },
  "fields": {}
}

I'm looking now how to create a document set in a specific folder in Sharepoint. For example, i want to create the following folder structure. The documents folder is at the library root and I want to create a document set named billing.

documents
|_ billing
   |_ 2021
      |_11
        |_01
           |_ document1.pdf
           |_ document2.pdf
           |_ document3.pdf
        |_02
           ...
        |_03
           ...
        |_04
      |_10
      ...

thanks

davidvera
  • 1,292
  • 2
  • 24
  • 55

3 Answers3

0

I'm doing something similar but I'm a little behind you, haven't yet created the Document Set (almost there!), but may I respectfully challenge your approach?

I'm not sure it's a good idea to mix Folders and Document Sets, mainly because a Folder breaks the metadata flow, you could achieve the same results just using Document Sets.

I am assuming that your 'day' in the data structure above is your Document Set (containing document1.pdf, etc.) You might want to consider creating a 'Billing' Document Set and either specifically add a Date field to the Document Set metadata or, perhaps better still, just use the standard Created On metadata and then create Views suitably filtered/grouped/sorted on that date.

This way you can also create filtered views for 'Client' or 'Invoice' or 'Financial Year' or whatever.

As soon as your documents exist in a folder, you can no longer filter/sort/group etc., the document library based on metadata.


FURTHER INFORMATION

I am personally structuring my Sales document library thus:

  • Name: Opportunity; Content Type: Document Set; Metadata: Client Name, Client Address, Client Contact

  • Name: Proposal; Content Type: Document; Metadata: Proposal ID, Version

  • Name: Quote; Content Type: Document; Metadata: Quote ID, Version

Etc...

This way the basic SharePoint view is a list of Opportunities (Document Sets), inside which are Proposals, Quotes etc., but I can also filter the view to just show Proposals (i.e. filter by Content Type), or search for a specific Proposal ID, or group by Client Name, then sort chronologically, or by Proposal ID etc.

I'm just saying that you get a lot more flexibility if you avoid using Folders entirely.

p.s. I've been researching for days now how to create Document Sets with graph, it never occurred to me that it might be a two-step process i.e. create the folder, then patch its content type. Many thanks for your post!!

David Adams
  • 83
  • 1
  • 1
  • 6
0

Just re-read your post and my assumption that the 'day' would be your document set was incorrect. In this case, there would be no benefit having a Document Set containing Folders because the moment a Folder exists in the Document Set, metadata flow stops, and the only reason (well, the main reason*) to use Document Sets in preference to Folders is that metadata flow.

*Document Sets also allow you to automatically create a set of documents based on defined templates.

David Adams
  • 83
  • 1
  • 1
  • 6
0

Just to add to the answers above, In step 3 where you are supposed to get the SharePoint Item Id, you might run into issues retrieving the sharepointid. This is caused by using the query parameter ?expand=sharepointids. To get the sharepoinid, simply change the expand to select.

for example, the previous implementation that can cause error:

    GET https://graph.microsoft.com/v1.0/sites/${siteId}/drives/${library.drive.id}/items/${folder.id}?expand=sharepointids

.

New implementation:

GET https://graph.microsoft.com/v1.0/sites/${siteId}/drives/${library.drive.id}/items/${folder.id}.Request().Select("sharepointIds");
Ucheuzor
  • 31
  • 6