Forum Discussion
jfergus11
Nov 13, 2020Copper Contributor
Programmatic way to add a shortcut to OneDrive for users?
Would like to add a SharePoint document library shortcut to our users OneDrive account within our organization. This is in reference to the relatively new feature of "Add shortcut to OneDrive". ...
Alex Pawlak
May 27, 2022Copper Contributor
Daniel Baird
I have managed to achieve it somehow
You need to register SPO App within Azure AD with Sites.FullControl.All permission, and look up shortcut body by inspecting browser dev tool (just to get the correct payload per folder). Add a link from browser and look for "children" POST API Call and copy the payload. There are possibly other ways to reconstruct the correct data, I was lazy so I looked up these values.
$UPN = "upn@domain.com"
$Token = (Get-MsalToken -TenantId tenantID -ClientId ClientID -ClientCertificate (get-item Cert:\LocalMachine\My\CertThumbprint) -scopes "https://yourtenant-my.sharepoint.com/.default").AccessToken
$URI = "https://yourtenant-my.sharepoint.com/_api/v2.1/drives/$UPN/items/root/children"
$Dokumenty = @(
@{Site1 = @"
{"name":"Documents","remoteItem":{"sharepointIds":{"listId":"listID","listItemUniqueId":"root","siteId":"SiteID","siteUrl":"https://yourtenant.sharepoint.com/sites/SiteName","webId":"32bf89e7-ae22-4d67-8444-698ed19421e7"}},"@microsoft.graph.conflictBehavior":"rename"}
"@},
@{Site2 = @"
{"name":"Documents","remoteItem":{"sharepointIds":{"listId":"listID","listItemUniqueId":"root","siteId":"SiteID","siteUrl":"https://yourtenant.sharepoint.com/sites/SiteName","webId":"32bf89e7-ae22-4d67-8444-698ed19421e7"}},"@microsoft.graph.conflictBehavior":"rename"}
"@},
@{Site3 = @"
{"name":"Documents","remoteItem":{"sharepointIds":{"listId":"listID","listItemUniqueId":"root","siteId":"SiteID","siteUrl":"https://yourtenant.sharepoint.com/sites/SiteName","webId":"32bf89e7-ae22-4d67-8444-698ed19421e7"}},"@microsoft.graph.conflictBehavior":"rename"}
"@}
)
$Dokumenty | % { Invoke-RestMethod -Uri $URI -Headers @{Authorization = "Bearer $Token"} -ContentType 'application/json' -Body $_.values -Method POST}
You need to replace the payloads within $Dokumenty array. Each Folder will be added for that user to OneDrive as Shortcut. Let me know if it worked for you 🙂
AbhishekKore
Mar 11, 2024Copper Contributor
Is there a way to get this working with Multi Geo tenant?
I keep getting error (308) when I try this script with user from another geo location apart from default geolocation.
I keep getting error (308) when I try this script with user from another geo location apart from default geolocation.