Download Library contents as PDF
Script to download files in a doclib to a local drive and convert Office Documents to PDF
Summary
Say we have lots of Office documents in a Document library. We need to have copies of the document stored on a local file share in case of network outages. We want the local copies to be in PDF format so people cant modify them.
This script will download the contents of a document library to a local directory , converting Office Documents to PDF in the process.
cls
# The client ID if an Azure AD app registration with graph read all sites app-only permission
# TODO: Enter value
$clientid=''
# The client Secret if an Azure AD app registration with graph read all sites app-only permission
# TODO: Enter value
$clientsecret=''
# The SharePoint tentant
# TODO: Enter value
$tenant="<tenant>.sharepoint.com"
# The azure tenant ID
# TODO: Enter value
$tenantid=""
# The Graph Drive ID found at https://tenant.sharepoint.com/sites/sitename/_api/v2.0/drives
# TODO: Enter value
$driveid=""
# The , three-part, Graph Site ID found at from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
# TODO: Enter value
$siteid="tenant.sharepoint.com,049287e5-abd9-472d-828b-a0a591ca2421,4d6b2467-70b2-46c6-a8a1-c8aa40f1bc9a" # from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
$filestoconvert='xlsx','docx','pptx'
$uri="https://login.microsoft.com/$tenantid/oauth2/v2.0/token"
$header = @{ ContentType = "application/x-www-form-urlencoded" }
$postbody = @{client_id=$clientid;scope='https://graph.microsoft.com/.default';client_secret=$clientsecret;grant_type='client_credentials'}
$authresponse = Invoke-RestMethod -Uri $uri -Headers $header -Body $postbody -Method Post
$token=$authresponse.access_token
$header = @{ Authorization = "Bearer $($token)" }
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/root/children"
do{
$response = Invoke-RestMethod -Uri $uri -Headers $header `
-Method Get -ContentType "application/json"
foreach ($item in $response.value){
Write-Host $item.name
if ($item.name.Contains('.')){
$fname=$item.name.Substring(0,$item.name.LastIndexOf("."))
$fext=$item.name.Substring($item.name.LastIndexOf(".")+1)
}else{
$fname=$item.name
$fext=''
}
if($filestoconvert.Contains($fext.ToLower())){
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/"+$item.id+"/content?format=pdf"
$outfile = "C:\temp\$fname.pdf"
} else
{
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/"+$item.id+"/content"
$outfile = "C:\temp\"+$item.name
}
Invoke-RestMethod -Uri $uri -Headers $header `
-Method Get -ContentType "application/json" -OutFile $outfile
}
$uri=$response.'@odata.nextLink'
}while ($uri -ne '')
# End
# The Graph Drive ID found at https://tenant.sharepoint.com/sites/sitename/_api/v2.0/drives
# TODO: Enter value
$driveid = ""
# The , three-part, Graph Site ID found at from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
# TODO: Enter value
$siteid = "" # from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
$filestoconvert = 'xlsx', 'docx', 'pptx'
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/root/children"
do {
$response = m365 request --url $uri | ConvertFrom-Json
foreach ($item in $response.value) {
Write-Host $item.name
if ($item.name.Contains('.')) {
$fname = $item.name.Substring(0, $item.name.LastIndexOf("."))
$fext = $item.name.Substring($item.name.LastIndexOf(".") + 1)
}
else {
$fname = $item.name
$fext = ''
}
if ($filestoconvert.Contains($fext.ToLower())) {
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/" + $item.id + "/content?format=pdf"
$outfile = "C:\temp\$fname.pdf"
}
else {
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/" + $item.id + "/content"
$outfile = "C:\temp\" + $item.name
}
m365 request --url $uri --filePath $outfile
}
$uri = ''
if ($null -ne $response['@odata.nextLink']){
$uri = $response['@odata.nextLink']
}
}while ($uri -ne '')Contributors
| Author(s) |
|---|
| Russell Gove |
| Adam Wójcik |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about the Microsoft Graph
Check out the documentation site to get started and for the reference to the endpoints.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.