I'm really new to Microsoft Graph API, I created a script using powershell to get reports from Microsoft 365 and it is being saved on my drive ( c:\temp\reports.xlsx
).
After it is being saved i wish to upload it to SharePoint online. On reading the docs, Microsoft says to do the following request,
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
I then tried to apply it to my use case and this was my request:
function RestMethod {
Param (
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Request,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$RequestMethod,
[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]$Body
)
$Headers = @{
Authorization = "$($global:RequestToken.token_type) $($global:RequestToken.access_token)"
}
$RestResults = $null
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$RestResults = Invoke-RestMethod -Uri $Request -Headers $Headers -Method $RequestMethod -ContentType "application/json"
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
return $RestResults
}
$upLoadRequest = "https://graph.microsoft.com/v1.0/sites/tenant.sharepoint.com/drive/items/Test:/$($File):/content"
$upLoadResults = RestMethod $upLoadRequest 'PUT'
The $File
variable contains c:\temp\reports.xlsx
the directory where my excel file is saved on getting the reports. The Test
in my Url is a folder I created in Documents
on my Site. But while doing the request I get this:
StatusCode: 400
StatusDescription: Bad Request
Please Help
Thanks
The list of changes:
Test
folder of Documents
library, it could be addressed like this:/v1.0/sites/{tenant}.sharepoint.com/drive/root:/Test/reports.xslt:/content
refer Addressing resources in a drive on OneDrive for a more details Upload
endpoint is missing , it could be provided via -InFile
parameter , eg Invoke-RestMethod -InFile $path...
Here is a minimal example:
$access_token = "--access token goes here--"
$path = "--path to local file, e.g. c:\data\report.xlsx--"
$url = "https://graph.microsoft.com/v1.0/sites/{tenant}.sharepoint.com/drive/root:/{folder}/{filename}:/content"
$headers = @{'Authorization' = "Bearer $access_token" }
Invoke-RestMethod -Uri $url -Headers $headers -Method Put -InFile $path -ContentType 'multipart/form-data'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.