Forum Discussion
Programmatic way to add a shortcut to OneDrive for users?
jfergus11 Did you find a workable solution for this?
- Alex PawlakMay 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 🙂
- AbhishekKoreMar 11, 2024Copper ContributorIs 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. - Mathieu80Dec 14, 2022Copper Contributor
Alex Pawlak I tried your solution and I cannot make it work with the App Registration. If I set $Token with the Token from a SharePoint session I have open in my browser, it works.
Can you provide the exact settings you put in the app registration? I selected SharePoint - Sites.FullControl.All (App Only). I don't know what's wrong.
- Craig2450Nov 16, 2022Copper Contributordid this work for you?