Microsoft’s Graph API has endpoints for [SharePoint Online] lists and libraries. Worthy of note, document libraries are /drives when using the APIs. And although libraries are technically lists, the two resources will return different properties for the same SharePoint object. Additionally, some actions can only be performed using the /lists resources. An example of this, file metadata CRUD operations using /drives. These operations require the use of /lists…
- E.g., function for single API request.
function Get-APIResponse {
param(
[System.String] $MicrosoftGraphAPIUrl = "https://graph.microsoft.com/v1.0",
[Parameter(Mandatory)] [System.String] $APIGetRequest
)
try {
$GraphAPI = "$MicrosoftGraphAPIUrl/$APIGetRequest"
$GraphReq = Invoke-RestMethod `
-Uri $GraphAPI `
-Headers (Get-GraphAPIHeader) `
-Method GET `
-ContentType "application/json"
}
catch [Exception] {
return $null
}
return $GraphReq
}
- E.g., function for batch API requests.
function Get-APIBatchResponse {
param(
[System.String] $MicrosoftGraphAPIUrl = "https://graph.microsoft.com/v1.0",
[Parameter(Mandatory)] [System.Array] $ThisBatch
)
try {
$jsonBody = @{
"requests" = $ThisBatch
} | ConvertTo-Json -Depth 5
$jsonBody | Out-Host
$GraphAPI = "$MicrosoftGraphAPIUrl/`$batch"
$GraphReq = Invoke-RestMethod `
-Uri $GraphAPI `
-Headers (Get-GraphAPIHeader) `
-Body $jsonBody `
-Method POST `
-ContentType "application/json"
}
catch [Exception] {
return $null
}
return $GraphReq
}
- E.g., API to query site ID.
[System.String] $SPOSitePath = "/sites/Portal"
Get-APIResponse -APIGetRequest "/contoso.sharepoint.com:$($SPOSitePath)"
- E.g., API to query list ID.
[System.String] $SPOSiteGuid = "contoso.sharepoint.com,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Get-APIResponse -APIGetRequest "/sites/$($SPOSiteGuid)/lists/?`$select=id,displayName,webUrl"
With the site and list IDs, individual items can be updated with a batch request…
- E.g., API to update list item metadata.
$listOf = @()
$listOf += @{
"id" = "1"
"method" = "PATCH"
"url" = "/sites/$($SPOSiteGuid)/lists/$($SPOListGuid)/items/$($SPItemGuid)/"
"headers" = @{
"Content-Type" = "application/json"
}
"body" = @{
"fields" = @{
"Title" = "$($SPOItemName)"
"MetadataField01" = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet"
"MetadataField02" = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet"
"MetadataField03" = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet"
}
}
}
Get-APIBatchResponse -ThisBatch $listOf
Conclusion:
The /drives resources are useful for other things, but not for metadata CRUD operations. That’s where the /lists pick up the slack. Likely because document libraries inherit from the list content type…
“None of us alone can save the nation or the world. But each of us can make a positive difference if we commit ourselves to do so.”
Cornel West
#blacklivesmatter