I have to upload file to SharePoint document library and then update file metadata. I am able to upload file but getting error while updating metadata. Here is my code,
Param (
$Tenant = "MY_TENANT_ID",
$ClientID = "CLIENT_ID",
$ClientSecret = "CLIENT_SECRETE",
$SiteID = "ed34d-3a2f-4ecf-a9c3-1bb8dc1a370a",
$LibraryURL = "https://xpd396.sharepoint.com/sites/testsite/myservicedocs",
$Path = ".\test.log"
)
#region get access token
$Body = @{
"client_id" = $ClientID
"client_secret" = $ClientSecret
"scope" = "https://graph.microsoft.com/.default"
"grant_type" = "client_credentials"
}
#$BodyJSON = $Body | ConvertTo-Json -Compress
$AuthorizationRequest = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$($Tenant).onmicrosoft.com/oauth2/v2.0/token" -Method POST -Body $Body -Verbose
#endregion
#region get drives
$Header = @{
"Authorization" = "Bearer $($AuthorizationRequest.access_token)"
"Content-Type"= "application/json"
}
$Result = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites/$SiteID/drives" -Method GET -Headers $Header -ContentType "application/json" -Verbose
$DriveID = $Result.value| Where-Object {$_.webURL -eq $LibraryURL } | Select-Object id -ExpandProperty id
If ($DriveID -eq $null){
Throw "SharePoint Library under $LibraryURL could not be found."
}
#endregion
#region upload file
$FileName = $Path.Split("\")[-1]
$fileUploadResult = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/drives/$DriveID/items/root:/$($FileName):/content" -Headers $Header -Method PUT -InFile $Path -ContentType 'multipart/form-data' -Verbose
$fileId = $fileUploadResult.id
#endregion
#region update metadata
$fieldUpdateUrl = "https://graph.microsoft.com/v1.0/drives/$DriveID/items/$fileId"
$fieldUpdateBody = @{
"Title" = "123"
}
$fieldUpdateBodyJSON = $fieldUpdateBody | ConvertTo-Json -Compress
$fileUploadResult = Invoke-RestMethod -Uri $fieldUpdateUrl -Method PATCH -Headers $Header -Body $fieldUpdateBodyJSON
#region
I am getting below error while updating metadata. Invoke-RestMethod : The remote server returned an error: (400) Bad Request
Maybe you want to update field
on the related listItem
$fieldUpdateUrl = "https://graph.microsoft.com/v1.0/drives/$DriveID/items/$fileId/listItem/fields"