Automatically Deploy OneDrive for Business Libraries

Posted by

Microsoft’s documentation for deploying OneDrive for Business to enterprise users is good.  One technical gap I found is our story on automatically deploying site and libraries for user’s after the OneDrive client is deployed.  Yes, it’s possible to automatically configure SharePoint Libraries to be sync’d by levering the ODOpen process and inputting a specific syntax.  The syntax provided by our documentation is the following:

odopen://sync/?siteId=SiteID_HERE&webId=WebID_HERE&listId=ListID_HERE&userEmail=UserEmail_HERE&webUrl=WebURL_HERE”

Two Challenges with coming up with this syntax:

  1. You’re required to know the Site ID, Web ID, List ID, and UPN\email of the current user.   This could require some additional work and in the past would need to write some CSOM to grab these values.
  2. In addition, you’re required to encode certain characters like the period, hyphen, and the at sign.

 

From here:

 

I authored some PowerShell to help SharePoint Online Administrators build out the syntax for a specified user and library.  I’m falling in love with the PnP SharePoint module and use it to call into SharePoint Online and grab the id’s required.  I added some additional logic to properly encode the special characters.  At a high level, my script does the following:

  1. Prompts for Site URL
  2. Prompts for List Name
  3. Prompts for User Email <- this is user that will be syncing
  4. Prompts for SharePoint Online Admin credentials

 

After grabbing the ID’s for both Site and List, it encodes the special character and provides the result.

I make two assumptions here so the following should be performed before continuing:

  • Sync client is already installed on the client
  • User has already gone thru OneDrive Setup (this can be deployed by an Admin)

 

In my simple example, I have a user named Adam and I’m the SharePoint Online Administrator.   I need to generate the ODOpen syntax in order to automatically sync a library titled, “RandomDocLibrary” on Adam’s desktop.  This library contains two documents titled SampleDoc1 and SampleDoc2.

 

Also, Adam has already sync’d one library from the same site called myTestLib which contains two documents:

 

On my desktop, I have PnP SharePoint already installed so will run the PowerShell script.  Running the script looks like the following:

Part 1

 

Part 2 (After entering SPO Admin credentials)

This syntax can then be deployed by Admin team by leveraging login script or SCCM for the given user.  For testing purposes, I can manually run the syntax directly from Windows PowerShell on Adam’s Windows 10 Client:

 

Initiate the command on the client manually

 

Results of above action

After hitting enter key, I see the toaster pop-up indicating it’s syncing the library:

 

Within Windows Explorer, the RandomDocLibrary is present:

 

This is a simple scenario and feel free to add any additional custom logic you want.  Let me give a few examples of how you could extend the above functionality of the script:

Example 1:  I have a list of users and want to sync one or more libraries for a given site.  I would probably add import-csv and iterate each user and output the odopen syntax for each user to CSV.  How you deploy the odopen syntax for each user is up to you.  This is where I would probably work with my Active Directory team or SCCM team to go over options.

 

Example 2:  I want run the above PowerShell on each client at login and automatically sync libraries.   In this case, I would need to add the following logic in my script:

  1. Grab the current user UPN value.  Could do something like $UPN = whoami /upn
  2. Include all of the sites/libraries I want to sync as part of the script
  3. For each site/library, it would generate ODOpen syntax
  4. I would then run the ODOpen syntax by running something like:

start “Odopen syntax in quotes”

 

If you’re interested in a sample script of either example above, feel free to mention that in comments section and I’ll provide a sample in a part2 blog.

Prerequisite:  The Pnp Module for SharePoint must be installed on the client.  Yes, the script can run as a user with contribute permissions to the target SharePoint site and it will still retrieve the ID’s with no issues.

 

Instructions for running the script:

1. Copy the below script and save it in notepad
2. Save it with a anyfilename.ps1 extension
3. To run, copy the file to a SharePoint Server
4. Select Start\Microsoft SharePoint 201x Products\SharePoint 201x Management Shell
5. Browse to directory holding the copied script file
6. Run the script: .\anyfilename.ps1 (assuming anyfilename is the name of the file)

 

Script is below:

<# ==============================================================
//
// Microsoft provides programming examples for illustration only,
// without warranty either expressed or implied, including, but not
// limited to, the implied warranties of merchantability and/or
// fitness for a particular purpose.
//
// This sample assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Microsoft support professionals can help explain the
// functionality of a particular procedure, but they will not modify
// these examples to provide added functionality or construct
// procedures to meet your specific needs. If you have limited
// programming experience, you may want to contact a Microsoft
// Certified Partner or the Microsoft fee-based consulting line at
// (800) 936-5200Call: (800) 936-5200.
//
// For more information about Microsoft Certified Partners, please
// visit the following Microsoft Web site:
// https://partner.microsoft.com/global/30000104
//
// Author: Russ Maxwell (russmax@microsoft.com)
//
// ———————————————————- #>

##Version 1.0 – Date 8-6-18##

##############################
##Get a proper ODOpen Syntax##
##############################
##For Example: odopen://sync/?siteId=SiteID_HERE&webId=WebID_HERE&listId=ListID_HERE&userEmail=UserEmail_HERE&webUrl=WebURL_HERE”##

###################
#Encoding Function#
###################
function encodeME($convertDots)
{
$COD = $convertDots
[System.Char]$charVal = “.”
$num = 0

$newCOD

while($num -lt $COD.Length)
{

if($COD[$num] -ne $charVal)
{
$newCOD = $newCOD + $COD[$num]
$num++
}

elseif($COD[$num] -eq $charVal)
{
#this logic works up to here
$newCOD = $newCOD + “%2E”
$num++
}

}

[System.String]$strnewCOD = $newCOD
return $strnewCOD
}

######################
##Script starts here##
########################
#Need to grab site URL from user#
Write-Host “Input URL of the SharePoint Site and press Enter”
$siteURL = Read-Host;Write-Host

#Need the library name#
Write-Host “Input name of library to sync and press Enter”
$listNameTMP = Read-Host;Write-Host
$listName = “/” + $listNameTMP

#Enter email address (UPN) of user#
Write-Host “Enter credentials of user that will sync and select Enter” -ForegroundColor Yellow
Write-Host “Username should be in UPN/Email format” -ForegroundColor Yellow
Write-Host
$UPN = Read-Host;Write-Host

#Need to connect to site#
Write-Host “Please authenticate to the site using SPO Admin Credentials”;Write-Host
Read-Host “Press Enter Key to get started”
$cred = get-credential

if($siteURL, $libName, $UPN -ne $null)
{
Connect-PnPOnline -url $siteURL -Credentials $cred

#Grabbing Site, Web, and List ID’s
$site = Get-PnPSite -Includes Id, URL
$siteIDtmp = $site.ID.toString()
#Adding some encoding here#
$siteID = “%7B” + $siteIDtmp + “%7D”

$web = Get-PnPWeb -includes Id, URL
$webIDtmp = $web.ID.toString()
#Adding some encoding here#
$webID = “%7B” + $webIDtmp + “%7D”

$list = Get-PnPList -Identity $listName -includes Id
$listIDtmp = $list.ID.toString()
#Adding some encoding here#
$listID = “%7B” + $listIDtmp + “%7D”

if($siteID, $webID, $listID)
{
#update encoding for UPN and SiteURL
[System.String]$newtmpSiteURL = encodeMe($siteURL)
[System.String]$newSiteURL = $newtmpSiteURL.Split(”,[System.StringSplitOptions]::RemoveEmptyEntries)
[System.String]$newtmpUPN = encodeMe($UPN)
[System.String]$newUPN = $newtmpUPN.Split(”,[System.StringSplitOptions]::RemoveEmptyEntries)

$resultTMP = “odopen://sync/?siteId=” + $siteID + “&webId=” + $webID + “&listId=” + $listID + “&listTitle=” + $listNameTMP + “&userEmail=” + $newUPN + “&webUrl=” + $newSiteURL

#Do remaining encoding work now#
$resultTMP2 = $resultTMP -replace “-“, “%2D”
$result = $resultTMP2 -replace “@”, “%40”

Write-Host $result
}
}

else
{Write-Host “Missing one of the requested values! Please run script again and insert correct values”;return}

End of Script

 

Reference: https://docs.microsoft.com/en-us/onedrive/deploy-on-windows

 

Thanks!

Russ Maxwell, MSFT