1

I am using below script to create a folder in my SharePoint library but I am getting error as Invalid request. I followed graph api as per documentation but couldn't find what is wrong in here. Any clue??

Function GET-Folder
{
    param($clientId, $clientSecret)
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadFolderRequestHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadFolderRequestHeaders.Add("Content-Type", "application/json")
  
    $uploadFolderRequestHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)
    $uploadFolderRequestBody = @{
name= "NewFoldew2r"
folder = {}
} | ConvertTo-Json
    $fullname = "https://graph.microsoft.com/v1.0/sites/$($SPid)/drives/$($LibId)/root/children/"
    write-host "test"
    
    $uploadFolderResponse = Invoke-RestMethod $fullname -Method 'POST' -Headers $uploadFolderRequestHeaders -Body $uploadFolderRequestBody
    $uploadFolderResponse | ConvertTo-Json
    return $uploadFolderResponse
}

latest Error:

    2020-12-09T20:00:52.4660142Z Invoke-RestMethod : {
2020-12-09T20:00:52.4661057Z   "error": {
2020-12-09T20:00:52.4661551Z     "code": "invalidRequest",
2020-12-09T20:00:52.4662315Z     "message": "Invalid request",
2020-12-09T20:00:52.4662994Z     "innerError": {
2020-12-09T20:00:52.4663189Z       "date": "2020-12-09T20:00:52",
2020-12-09T20:00:52.4663531Z       "request-id": "99c22009-9bcb-4c3b-acb7-cd697a5040a9",
2020-12-09T20:00:52.4663926Z       "client-request-id": "99c22009-9bcb-4c3b-acb7-cd697a5040a9"
2020-12-09T20:00:52.4664153Z     }
2020-12-09T20:00:52.4664541Z   }
2020-12-09T20:00:52.4664659Z }
2020-12-09T20:00:52.4664946Z At D:\a\r1\a\_DevOpsScripts\ReleaseNoteScripts\UploadInSharePoint.ps1:103 char:28
2020-12-09T20:00:52.4665342Z + ... rResponse = Invoke-RestMethod $fullname -Method 'POST' -Headers $uplo ...
2020-12-09T20:00:52.4665666Z +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-12-09T20:00:52.4666140Z     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
2020-12-09T20:00:52.4666515Z    eption
2020-12-09T20:00:52.4666815Z     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
2020-12-09T20:00:52.6029759Z ##[error]PowerShell exited with code '1'.
2020-12-09T20:00:52.6394118Z ##[section]Finishing: PowerShell Script
7
  • (1) I would try the above call in graph explorer and see if it works Commented Dec 9, 2020 at 19:33
  • it is used in $uploadFolderRequestBody and i am calling that in body in Invoke method Commented Dec 9, 2020 at 19:36
  • it works in graph explorer Commented Dec 9, 2020 at 19:37
  • If it works in Graph explorer then the Graph API call works. Looks like the above script is not sending the request/payload properly due to that its failing... Commented Dec 9, 2020 at 19:38
  • i logged each variables, all seem to be correct, if there was issue with request the error message would hv been different Commented Dec 9, 2020 at 19:42

1 Answer 1

1

The error you are getting is because you are assigning different properties to folder property in the body when you use {} in powershell. The document specifies the example in the JSON format where {} means an empty object.

If you print $uploadFolderRequestBody you would see the data as below in the folder property.

enter image description here

So please give the body data as below.

$uploadFolderRequestBody = @{
name= "NewFoldew2r"
folder = null
"@microsoft.graph.conflictBehavior"= "rename"
} | ConvertTo-Json

And now run the Invoke-RestMethod.You will be getting proper results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.