0
votes

I am trying to upload a local PDF file into share point via using Graph API but the PUT method is just letting me to create a new file in the SharePoint but not letting me upload a local existing C:/ Drive PDF file

Here is the API that I am using

PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/SharePointFilePath/file-name:/content

2

2 Answers

0
votes

As per here, that is the correct endpoint. however, you need to send ithe file as a binary stream in the body. https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

This means that you need to read your local pdf file into a binary stream and send it to the endpoint in the body of the put request. as a test for example, you could create a regular text file with the body as string and contenttype as text.

also if it's larger than 4MB, then you need to use a different method. https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession mainly an uploadsession.

0
votes

Previous test script to upload file to SharePoint by PnP PowerShell.

https://social.technet.microsoft.com/Forums/msonline/en-US/25191b51-41dd-4f4b-93aa-a46594c9a184/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api?forum=onlineservicessharepoint

Connect-PnPOnline -AppId 'yourAzure app id' -AppSecret "yourAzure app secret" -AADDomain 'xxx.onmicrosoft.com'
$accessToken= Get-PnPAccessToken
$apiUrl = "https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,x,x/drives/driveid/items/01AKXHS4ELSEOTLPZHZZDYYBOW57WR6HK6:/test.xlsx:/content"
$path="C:\Lee\Script\testdata.xlsx"
$file=[IO.File]::ReadAllBytes($path)
Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Put -Body $file -ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"