Home > Blockchain >  Graphi API - Creating Document Set with Invoke-RestMethod
Graphi API - Creating Document Set with Invoke-RestMethod

Time:07-12

I need some help to get my code to create a Document Set in SharePoint Online using Graph API directly from a PowerShell script using Invoke-RestMethod.

I tested the request under the Graph Explorer portal and it works fine and I get a nice HTTP 201 (OK) as seen on the picture below:

enter image description here

Trying the very same request from my PowerShell script fails and returns HTTP 400 (Bad Request), I can't get the folder created and that is the first step to get the document set created, according to my research and an example found here:

Is it possible to create a project documentset using graph API?

As the first step mentioned in the example above, I need to first create the folder and then proceed to the following steps to achieve the creation of the document set but I can't get this first step done.

My application has the necessary permissions as I tested in the Graph Explorer:

  • Files.ReadAndWrite.All
  • Sites.ReadAndWrite.All
  • Sites.FullControl.All (not required but I had to try this one to make sure!)

I'm on the second step (folder creation) and I can't get past this point, according to the link above, once I get this working I will need to get the new folder ID, and then send a new PATCH to alter its content type to match the desire document set, I hope I can get some help, all the examples are vague and pretty much describe only on what to do but no actual functional code to sample from.

Thanks in advance!

$uri = "https://graph.microsoft.com/v1.0/drives/b!yVnguUBzyUC1PxgTM0JP-_ERFp1PTZFCjycaWZK6yKulBi9Ce_J8RIfF-OkWKE4B/root/children"

$headers = @{
                "Authorization" = "$($token.token_type) $($token.access_token)"
                "Content-Type"  = "application/json"
            }

$body = @{
            "name"   = "Test"
            "folder" = {}
            "@microsoft.graph.conflictBehavior" = "rename"
         }

$request = Invoke-RestMethod -Headers $headers -Body $body -Method Post -Uri $uri

CodePudding user response:

It should work when you modify $body like this

$body = @{
        "name"   = "Test"
        "folder" = @{}
        "@microsoft.graph.conflictBehavior" = "rename"
     } | ConvertTo-Json

$body is a JSON object and you need to convert it to JSON. For initialing an empty folder object you have to use @{} instead of {}.

CodePudding user response:

Thanks @user2250152, you gave me a great idea by solving part of the problem!

I did add the conversion to JSON as you recommended and decided to add the content-type back to my original header and it did the trick!

Adding the content-type solved the issue but your collaboration was essential, so thank you very much!

Now wish me luck to get the other steps done and achieve the conclusion of this thing!

$headers = @{
                "Authorization" = "$($token.token_type) $($token.access_token)"
                "Content-Type"  = "application/json"
            }
  • Related