0

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
Mr. Dr. Sushi
  • 469
  • 8
  • 22

2 Answers2

0

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 {}.

user2250152
  • 14,658
  • 4
  • 33
  • 57
  • thanks! I did try all sorts of variations including the @{ } - I felt happy that my hunch was at least in the right direction for "folder", the only thing missing on my attempts was to convert the $body to a JSON - unfortunately it didn't work, I still get an HTTP 400 even after using your example on my code – Mr. Dr. Sushi Jul 11 '22 at 09:26
0

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"
            }
Mr. Dr. Sushi
  • 469
  • 8
  • 22