Forum Discussion

jfergus11's avatar
jfergus11
Copper Contributor
Nov 13, 2020

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".  

 

Would like to do this automatically.  Is this possible via powershell or any API?

Thanks!

    • Alex Pawlak's avatar
      Alex Pawlak
      Copper 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's avatar
        AbhishekKore
        Copper 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.
  • jfergus11 
    You can do that in an old fashioned way via Group Policy
    But read it carefully, there is a limit:  5000 Files/folders and 1000 devices

     

    I do not know, but I think it's not in the API right now.

    If you have more the 1000 Devices (nut users!) , you have to do it as described below,

     

    But You can share a Link to all users from the document folder of the SharePoint Library
    And the inform the users, the should go into their OneDrive , Klick on Shared with me. Identify the Folder, Select the folder and the click on Add a ShortCut to My Files. Done

    Is that too much for the users?

    • helloitsliam's avatar
      helloitsliam
      MVP

      HansBrender From testing, the only issue I see is that the group policy piece did not work for communication sites, only for Team Sites. Can you confirm? Also no PowerShell or Graph API yet? Is that still the case? Thanks

      • HansBrender's avatar
        HansBrender
        MVP

        helloitsliam So the Group Policy is a technology, which is limited, because this is the root folder (and if you have a lot of documents in this library, you can got in trouble with too many files (300000 is the Limit)

        Add to shortcut is a quick thing, each person will do manually and has many advantages.
        you may read my last article about Add to OneDrive , what's the difference.

Resources