Forum Discussion
Add link to SharePoint Document Library to OneDrive programmatically with PowerShell or PA
Hello,
I have a document library on a SharePoint Online Site that needs to be added as a shortcut in each user's OneDrive.
Obviously, you can do this individually as the user, but I would like to add this link to each user's OneDrive now, and every time a new user account is created in the future(Will take care of this later). This is required as the leadership for my organization wants users to be able to view the library from their file explorer, but open the file in their browser. (Add Shortcut to OneDrive functionality). The Sync functionality is not acceptable.
The "1C" folder here was added by clicking the "Add Shortcut to OneDrive" button on the SPO Document library itself. I would like to achieve the same result, just programmatically for each user.
Any advice is greatly appreciated. I am aware of how to create new files in Power Automate and PowerShell, but cannot seem to create the same link as achieved with the "Add Shortcut to OneDrive" button.
Thank you in advance for your time,
Cam
9 Replies
- BartKuppensCopper Contributor
Hi there, I know this is an old thread but I was looking for the exact same solution and found a post somewhere sharing a solution. This solution didn't work though. It was purely theoretical and based on what that person found by using the developer dashboard and inspecting the calls that were made when someone actually uses that functionality from SharePoint. An HTTP POST call is made to https://tenantname-my.sharepoint.com/_api/v2.1/drives/me/items/root/children with a specific payload:
They proposed to replace the "me" in the request with the UPN of the user you want to target. The thing is, that API does not accept UPN's. It's only for "me".
BUT... there IS a different endpoint you can use that does target other users and that's this one:https://graph.microsoft.com/v1.0/users/$upn/drive/root/children
And if you send that exact payload to this endpoint... the shortcut is created.
So, here's how i did it in PowerShell.$upn = "email address removed for privacy reasons" $assertionToken = GetClientCertificateAssertionToken -Certificate $certificate -TenantId $tenantId -AppId $clientId $body = @{ Grant_Type = "client_credentials" Scope = "https://graph.microsoft.com/.default" client_Id = $clientId client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" client_assertion = $assertionToken } $tokenresponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method POST -Body $Body -ContentType "application/x-www-form-urlencoded" $shortcutUrl = "https://graph.microsoft.com/v1.0/users/$upn/drive/root/children" $shortcutBody = @{ "name" = "Documents" "remoteItem" = @{ "sharepointIds" = @{ "listId" = "dece8bcb-ee96-445a-97ac-e9929f18dcc8" "listItemUniqueId" = "root" "siteId" = $siteId "siteUrl" = "https://$tenantname.sharepoint.com/sites/TestTeam" "webId" = "f8a11726-4760-4621-9634-88b58dfc607b" } } "@microsoft.graph.conflictBehavior" = "rename" } | ConvertTo-Json -Depth 3 $response = Invoke-RestMethod -Uri $shortcutUrl -Headers @{Authorization = "Bearer $($tokenresponse.access_token)"} -ContentType 'application/json' -Body $shortcutBody -Method POST
You obviously need an app registration with the correct Microsoft Graph API permissions (Sites.ReadWrite.All, Files.ReadWrite.All, and User.ReadWrite.All).
Tested. Works!
Perhaps this answer can serve as a reference for other people looking for the same thing.- Suleyman AliIron Contributor
This absolutely worked for me. I used the $shortcutUrl and $shortcutBody snippet with invoke-mggraphrequest (after using certificate based auth using app registration). I have been waiting for this for a good few years now. This will make my new user onboarding scripts THAT much better ;)
- OwenHarveyCopper Contributor
Hi,
Does this still work for you? I'm testing right now, and that endpoint does not support POSTs. I searched around and only see PUTs, which require file content.
- cwdgomezCopper ContributorThe best option to achieve this is to use a third-party scalable utility. I have found IAM Cloud Drive Mapper works the best. Zee Drive is another option, and what both do is aggregate based on your permissions, which SharePoint site you have permission to, and you can map them as a single Drive letter for your user base automatically. In addition, you can map OneDrive as a separate drive letter as well. Check it out: https://www.iamcloud.com/cloud-drive-mapper/
- Lalit MohanIron ContributorTo add a link to a SharePoint Document Library to OneDrive programmatically using PowerShell, you can utilize the SharePoint PnP PowerShell module. Here's an example script: powershell Copy code # Install the SharePoint PnP PowerShell module if not already installed Install-Module -Name SharePointPnPPowerShellOnline -Force # Connect to SharePoint Online Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Credentials (Get-Credential) # Define the necessary variables $documentLibraryName = "Documents" $documentLibraryRelativeUrl = "/sites/yoursite/Shared Documents" # Relative URL of the document library $linkName = "Shared Documents Link" $linkUrl = "https://yourtenant.sharepoint.com/sites/yoursite/Shared Documents" # Create the link in the OneDrive Add-PnPLink -List $documentLibraryName -WebRelativeUrl $documentLibraryRelativeUrl -Title $linkName -Url $linkUrl Make sure to replace the following placeholders with your actual information: "https://yourtenant.sharepoint.com/sites/yoursite": Replace with the URL of your SharePoint site. "Documents": Replace with the name of your document library. "/sites/yoursite/Shared Documents": Replace with the relative URL of your document library. "Shared Documents Link": Replace with the desired name for the link in OneDrive. "https://yourtenant.sharepoint.com/sites/yoursite/Shared Documents": Replace with the URL of the SharePoint document library. Once you've updated the script with the correct information, you can run it in PowerShell to create the link in OneDrive pointing to the SharePoint Document Library.
- CamCovCopper ContributorAdd-PnpLink is not part of any cmdlet. ChatGPT is great but often inaccurate, please test your solutions before replying to people.
- GPO policy sounds like what you're really looking for and then push it out when IT/Manager decides ?
https://learn.microsoft.com/en-us/sharepoint/use-group-policy#configure-team-site-libraries-to-sync-automatically- CamCovCopper ContributorHello, I appreciate your help, but this describes the Sync functionality which behaves differently than the shortcut functionality.
- Yeha unfortunately there's nothing native that supports to push it to all, so it has to be a script and it's gonna be abit of a hassle.
What you can try this module, I've not tried it myself:
https://github.com/derpenstiltskin/onedriveshortcuts
@Lalit
I'm guessing ChatGPT response ? 🙂 As Add-PnPLink doesn't exist