Automatically mapping SharePoint sites in the OneDrive for Business client

Microsoft recently announced that the OneDrive for business client will support automatically mapping SharePoint sites – Which is something I’ve been working on to script for some time now. Automapping sites will be a great benefit for migrations where users are saying goodbye to the good old file system, and hello to SharePoint online. Combined with Files on Demands most users won’t even know the difference.

The only sad part about it is that my pretty cool logon script will become useless fairly soon, anyway I’m sharing this to everyone. Its still in a pretty unfinished state, but it might help you if you are currently struggling with this:

<#
.SYNOPSIS
Automaps the Onedrive client to sync with a sharepoint library


.DESCRIPTION
Run the script with all mandatory parameters

In order to use the parameters you need to fetch them from the applicable site.
Every _api link should have tenantname.sharepoint.com/ in front of it if it's the default site, or tenantname.sharepoint.com/sites/SITENAME for any other site.
If the _api data is hard to read, use a tool like https://codebeautify.org/xmlviewer


.PARAMETER siteid
_api/web
Search for "D:Id", here you will find the SiteID Guid


.PARAMETER webid


.PARAMETER listid
_api/web/Lists/getbytitle('NAMEOFTHELIBRARY')
Search for "D:Id", here you will find the ListID Guid


.PARAMETER URL
_api/web
Search for "D:web", here you will find the SiteID Guid


.PARAMETER webtitle
Set this to the name of the organisation

.PARAMETER listtitle
Set this to the name of the library

.EXAMPLE

.\SharePoint_Automapping.ps1 -siteid GUID -webid = "GUID" -listid = "GUID" -URL https://company.sharepoint.com/ -webtitle Company -listtitle Documents

which will create the following link for the OneDrive app:

start "odopen://sync/?siteId=GUID&webId=GUID&listId=GUID&userEmail=$upn&webUrl=https://company.sharepoint.com/&webtitle=Company&listtitle=Documents" 



.NOTES
In order to retrieve the _api info you need to log in to the tenant, no admin rights are necessary.


#>
param(
[string]$siteid,
[string]$webid,
[string]$listid,
[string]$URL,
[string]$webtitle,
[string]$listtitle
)

$rand = Get-Random -Maximum 10
sleep $rand
#Sleep is required, if the same script runs at the same time it'll mess up the configuration.
$Version = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ReleaseID | Select-Object ReleaseID
if($version.releaseID -lt 1709) { break }

$strFilter = “(&(objectCategory=User)(SAMAccountName=$Env:USERNAME))”
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = “Subtree”
$objSearcher.PropertiesToLoad.Add(“userprincipalname”) | Out-Null
$colResults = $objSearcher.FindAll()

$UPN = $colResults[0].Properties.userprincipalname
$path = "C:\Users\$($env:username)\Lime Networks BV\Lime Networks - $($listtitle)"
if(Test-Path $path){
#DoNothing
} else {
start "odopen://sync/?siteId=$siteid&webId=$webid&listId=$listid&userEmail=$upn&webUrl=$URL&webtitle=$webtitle&listtitle=$listtitle" 
}

20 Comments

  1. Mr J May 30, 2018 at 9:02 am

    Nice script and description, but I am still having trouble finding some of the IDs.

    Browsing this team site page that I want to sync
    https://tenantname.sharepoint.com/teams/team-test/_api/web
    Gives me the possibility to get d:Id, which will be the siteID
    11111111-2222-3333-4444-ec22ed931691

    Browsing the document library of the team site page
    https://tenantname.sharepoint.com/teams/team-test/_api/web/Lists/getbytitle('Document‘)
    Returns another d:ID, which would be ListID
    11111111-2222-3333-4444-cf2b7de15b57

    But where can I find the webID?

    I can brows this endpoint of the api
    https://tenantname.sharepoint.com/teams/team-test/_api/web/id

    But it gives me the same ID as the siteID

    Can you give me a hint? /J

    1. Matt Lewis December 12, 2018 at 12:37 am

      Try clicking the sync button in Internet Explorer, this gives me a popup window confirming the URL to sync. Coping and pasting this into notepad gives all the data required. You will need to convert the URL encoding though.

      1. Kelvin Tegelaar December 12, 2018 at 1:05 pm

        Yes! recent updates to SharePoint online allow you to find this data easier. When this article was posted this was not an option yet.

        There should also be a way to use the registry and GPO now for auto-configuration, but I haven’t had the chance to update this blog reflecting this. 🙂

  2. Ross Copland October 7, 2018 at 12:46 pm

    @Kelvin, thanks very much for your post, I managed to get this working in my environment with a bit of tweaking.

    1. Clarysse Thijs October 11, 2018 at 9:13 am

      Dear Ross,

      I tried this also but I keep getting error that it is not able to open it.
      I have resolved the id’s but i still get errors that it is not possible to add the library.

      Can you share the tweaking?

    2. Kelvin Tegelaar December 12, 2018 at 1:05 pm

      No problem! glad it works.

  3. Sharepoint Trainer October 13, 2018 at 9:33 am

    After checking the other links its easier to understand. thanks for the script.

  4. Janus Hallenborg Stampe October 31, 2018 at 2:09 pm

    Nice script.
    I am getting a bothersome error, “either the domain doesn’t exist, or a connection to it, could not be made”.

    I “think” i have done all the agruments correctly. Can you assist?

    1. Dany June 1, 2021 at 8:37 am

      Hey this you figure this out?

  5. Matt Lewis December 12, 2018 at 12:31 am

    To get the UPN for users not connected to Active Directory (Azure AD Users), grab the email address for the configured OneDrive Account.

    #Get the UPN from the synced OneDrive Account.
    $UPN = (Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts\Business1 -Name UserEmail).userEmail

  6. Paul January 17, 2019 at 8:56 pm

    I’ve got a working solution for this in python. The sourcecode can’t be made public unfortunately, since there’s a lot more to it, however I can explain the way it functions.
    First of all, you need to configure the OneDrive Client through the registry (“HKCU\SOFTWARE\Microsoft\OneDrive” set “EnableADAL” to 1)

    Then you need to get all the items required for formatting a string for each site Url.
    Using New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) you can obtain SiteId, WebId, ListId and ListTitle

    The result string will look something like below (listTitle is the display name for the library in Explorer, you can change it if you want to avoid duplicate names) “odopen://sync/?siteId=&webId=&listId=&listTitle=&userEmail=&webUrl=”

    Finally, replace spaces with %20, – (minus) with %2D and @ with %40 in your string, then write each string line by line to a textfile as ps1, and let it execute (use psrun.exe to get it run silently)

    Getting them connected was a bit of a strugle, however i’m now trying to get them automatically disconnected. I would expect it wouldn’t be too hard, however so far no luck…

    1. Kelvin Tegelaar January 17, 2019 at 9:15 pm

      There is now a slightly simpler way to get the odopen URL, although its still a manual operation; when opening the sharepoint site and clicking on the “Sync” button, small pop contains the option “Copy this library to sync automatically”.

      This actually copies the entire odopen link for that library and you are able to use it with the script I’ve posted above.

  7. Eric February 15, 2019 at 11:20 pm

    This is awesome, great job and thanks for sharing.

  8. John M. May 7, 2019 at 10:14 am

    Thanks Kevin,

    Indeed via IE11 you can simply copy the complete odopen URI with all relevant IDs and simply execute that.

    I found a small typo in your script $path = “C:\Users\$($env:username) was missing a \ between Users and $.

    thx for sharing, helped me tremendously.

    1. Kelvin Tegelaar May 8, 2019 at 7:52 am

      Thanks! I’ve fixed the typo 🙂

  9. Jason Beer August 26, 2019 at 9:26 pm

    Thank you so much for this article!

    The process for finding the UPN wasn’t working for me. If you are sure that the user’s machine is AzureAD joined, you could also use the following line to get their email from the registry.

    $UPN = (Get-ItemProperty -Path “HKLM:\SYSTEM\ControlSet001\Control\CloudDomainJoin\JoinInfo\*”).UserEmail

  10. Anthony April 29, 2020 at 9:58 pm

    Worked great thank you so much!! I am pretty new to power shell so go stuck on where/how to enter the string. After a little digging this worked for me:

    [string]$siteid = “Enter Site ID Here”,
    [string]$webid = “Enter Web ID Here”,
    [string]$listid = “Enter List ID Here”,
    [string]$URL = “Enter URL Here”,
    [string]$webtitle = “Enter Web Title Here”,
    [string]$listtitle = “Enter List Title Here”

    Thanks again!!

  11. Pingback: Automating with PowerShell: Teams Automapping - CyberDrain

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.