Get Drive ID and Drive Item ID for File for Further Microsoft Graph Operations using PnP PowerShell
Introduction
When working with files in SharePoint, the drive ID and drive item ID are essential parameters for further manipulation using Microsoft Graph API. The drive ID is a base64 encoded string composed of the site ID, web ID, and list ID for a particular SharePoint library. For more details, refer to Microsoft Graph: Encoding and decoding the drive ID by Mikael Svenson. The item ID part appears to be a base32 encoding of the SharePoint item’s unique ID, though the exact mechanics are still being explored.
Example: Get Details of a Drive Item ID
To retrieve details of a drive item using its ID, you can use the following Microsoft Graph API endpoint:
https://graph.microsoft.com/v1.0/drives/b!xd3Nn369IEG5NL9nW3aFX3yuncW9SEFClre4HUu8Jcu5BJ4LFx1NS4AG9d5QbHz8/items/01KSLV2CXBTFU4HFS2RBGY66M3IX3IFXSR
Script to Get Drive ID using Graph API
This sample script is to unlock/lock files declared as records using PnP PowerShell. Similar outcomes can be achieved using Microsoft Graph PowerShell (view post Get SharePoint Files with MS Graph for examples) and CLI for Microsoft 365.
$siteUrl = Read-Host -Prompt "Enter site collection URL";
$list = Read-Host -Prompt "Enter library name";
$itemid = Read-Host -Prompt "Enter file id";
$setLock = Read-Host -Prompt "Enter Yes or No for lock or unlock the file";
function Get-UrlComponents {
param (
[Parameter(Mandatory = $true)]
[string] $fullPath
)
# Regular expression to match the site, library, and document path
#In summary, this regex matches a string that starts with a /, followed by three segments separated by /, and then captures the rest of the path. Each segment is captured into a separate group for further processing.
$regex = "^/([^/]+)/([^/]+)/([^/]+)/(.+)$"
if ($fullPath -match $regex) {
$base = $matches[1]
$siteUrl = "/$base/$($matches[2])"
$library = $matches[3]
$documentPath = $matches[4]
return @{
SiteUrl = $siteUrl
Library = $library
DocumentPath = $documentPath
}
} else {
throw "Invalid path format. Expected format: /<base>/<site>/<library>/<document path>"
}
}
Connect-PnPOnline -url $siteUrl -Interactive
#get site id
# Extract the domain and site name
$uri = New-Object System.Uri($siteurl)
$domain = $uri.Host
$siteName = $uri.AbsolutePath
$RestMethodUrl = "v1.0/sites/$($domain):$($siteName)?$select=id"
$site = (Invoke-PnPGraphMethod -Url $RestMethodUrl -Method Get -ConsistencyLevelEventual)
$siteId = $site.id
#get drive id
$url = 'v1.0/drives/'+$driveId+'/root:/'+$($relativeUrl)+'?$select=id'
$drives = (Invoke-PnPGraphMethod -Url "v1.0/sites/${siteId}/drives?$select=webUrl,id" -Method Get).Value
#filter $drives by weburl matches $_listUrl
$driveId = $drives | Where-Object { $_.name -eq $list} | Select-Object -ExpandProperty id
$item = Get-PnPListItem -List $list -Id $itemid
#get drive item id
$path = $item.FieldValues["FileRef"]
$result = Get-UrlComponents -fullPath $path
$url = 'v1.0/drives/' +$driveId+'/root:/'+ $($result.DocumentPath) +'?$select=id'
$driveItemId =(Invoke-PnPGraphMethod -Url $url -Method Get).Id
write-host $driveItemId
$url = 'v1.0/drives/' +$driveId+'/items/'+$driveItemId+'/retentionLabel'
$bSetLock = $SetLock -eq "Yes" ? "true" : "false"
$Payload = @"
{
"retentionSettings": {
"isRecordLocked": $bSetLock
}
}
"@
#unlock the item
Invoke-PnPGraphMethod -Url $url -Method Patch -Content $Payload
Errors
If you encounter the following error:
Ensure that the item you are trying to unlock is indeed declared as a record.
ALternative script to Get Drive ID using PnP PowerShell
$siteUrl = Read-Host -Prompt "Enter site collection URL";
$list = Read-Host -Prompt "Enter library name";
$itemid = Read-Host -Prompt "Enter file id";
$setLock = Read-Host -Prompt "Enter Yes or No for lock or unlock the file";
Connect-PnPOnline -url $siteUrl -Interactive
#filter $drives by weburl matches $_listUrl
$item = Get-PnPListItem -List $list -Id $itemid -Includes File.VroomDriveID,File.VroomItemID
$url = 'v1.0/drives/' +$item.File.VroomDriveId+'/items/'+$item.File.VroomItemID+'/retentionLabel'
$bSetLock = $SetLock -eq "Yes" ? "true" : "false"
$Payload = @"
{
"retentionSettings": {
"isRecordLocked": $bSetLock
}
}
"@
#unlock the item
Invoke-PnPGraphMethod -Url $url -Method Patch -Content $Payload