Introduction:
We implemented an automated Document Library Sync mechanism with OneDrive Sync Client using PowerShell for Construction Engineering Company based out of Washington. Normally, we can sync a SharePoint Document Library with OneDrive Sync Client manually from the user interface very easily. But, here the requirement was to sync Document Library in a dynamic and automated manner with the combination of OneDrive Sync Client & PowerShell.
Scenario:
We were getting a CSV file with URLs of multiple document libraries generated every 24 hours. The requirement was to configure OneDrive Sync Client for URLs of Document Libraries received in the CSV file in an automated manner. So, we implemented a PowerShell script that will read Document Library URLs from the CSV file and configure OneDrive Sync Client for all the Document Libraries. This PowerShell script then was configured in Windows Task Scheduler for automated execution every 24 hours. Let's see the step-by-step process for this implementation with an example scenario.
Steps & PowerShell Script:
Here, we have taken a CSV file, where we are storing the URLs of the Document Libraries. Please check the below screenshot for the same.
Let’s consider one thing:
- URL: https://spsite.sharepoint.com/sites/SpaceDemo/A1
Here - - SpaceDemo = Name of the Site Collection
- A1 = Document Library Name
To configure the OneDrive Sync Client for a Document Library folder, we need to use the following command in PowerShell.
odopen://sync/?siteId={” + $siteId + “}&webId={” + $webId + “}&listId={” + $listId + “}&listTitle=” + $listName + “&userEmail=” + $UPN + “&webUrl=” + $siteURL + "&webTitle=" +$webTitle
Where -
- siteId = SharePoint site collection siteId GUID, enclosed in curly brackets. We can get this GUID visiting https://<TenantName>.sharepoint.com/sites/<SiteName>/_api/site/id
- webId = SharePoint site webId GUID, enclosed in curly brackets. We can get this GUID visiting https://<TenantName>.sharepoint.com/sites/<SiteName>/_api/web/id
- webUrl = SharePoint site URL.
- listId = SharePoint site documents library GUID, enclosed in curly brackets. We can get this GUID visiting the document library in the browser, click in the gear icon and choosing "Library Settings". The URL will show the listId GUID at the end of URL, i.e. https://<tenant>.sharepoint.com/sites/<SiteName>/_layouts/15/listedit.aspx?List=%7Bxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%7D (a GUID with escaped curly brackets).
- userEmail =OneDrive's user email address used to sign in into OneDrive.
- <webTitle> and <listTitle> are used to compose the name of the local folder where the OneDrive content is synchronized. By default, when we use the "Sync" button when in the browser to synchronize a document library, OneDrive uses the SharePoint site name and the document library name to compose the local folder name, in the form of %userprofile%\<TenantName>\<SiteName> - <DocumentLibraryName>. We can use any other values. If we do not use these parameters, the local folder will be named " - Documents", despite of site and library names.
Here, in this sample we will find all above parameters dynamically. Please
check the below script for the same.
Write-Host “Please Enter your UserName” -ForegroundColor Yellow
$Username = Read-Host;Write-Host
Write-Host “Please Enter your Password” -ForegroundColor Yellow
$Password = Read-Host -AsSecureString;Write-Host
Import-Csv C:\DOCLIB.csv | ForEach-Object {
Write-Host "$($_.DocLibURL)"
#Write-Host “Input URL of the SharePoint Site with DOC Library Name and press Enter”
$siteURL1 = "$($_.DocLibURL)" #Read-Host;Write-Host
$listName = $siteURL1.Substring($siteURL1.LastIndexOf("/")+1)
$siteURL = $siteURL1.Substring(0,$siteURL1.LastIndexOf("/"))
$webTitle = $siteURL.Substring($siteURL.LastIndexOf("/")+1)
#Read-Host;Write-Host
$UPN = $Username
$creds = (New-Object System.Management.Automation.PSCredential $Username,(ConvertTo-SecureString $Password -AsPlainText -Force))
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”
$resultTMP1 = “odopen://sync/?siteId={” + $siteIDtmp + “}&webId={” + $webIDtmp + “}&listId={” + $listIDtmp + “}&listTitle=” + $listName + “&userEmail=” + $UPN + “&webUrl=” + $siteURL + "&webTitle=" +$webTitle
Write-Host $resultTMP1
Start $resultTMP1
Write-Host "Completed for"+ $siteURL1 -ForegroundColor Green
}
else
{Write-Host “Missing one of the requested values! Please run script again and insert correct values”;return}
}
Test the Script Execution:
First, enter organizational email.
Enter Password and then it will ask for sync. Click on Sync now.
Once the PowerShell script is executed, this will show the following message.
Conclusion:
This is how we can sync SharePoint Document Library with OneDrive. Happy scripting!!
If you have any questions you can reach out our
SharePoint Consulting
team
here.
No comments:
Post a Comment