EnjoySharePoint YouTube Channel

Automate SharePoint Online Deployment using PowerShell and CSOM

We always think of having automation solution while deploying components in office 365 projects. I can also say it’s a really challenging moving list, libraries, all out of the box development from Dev to other sites.

Get Office 365 Enterprise E3 Or Business Premium Subscription & Try out all the features

Using PowerShell and CSOM Combination, we can write a script to automate the deployment process.

I have implemented a few of the scripts to automate the deployment process, which I will be explaining in further articles.

Let’s start with “How to connect Office 365 site from PowerShell editor?”

We have to prepare our machine to write a script. Here is an interesting part you can write the scripts in SharePoint (On-Premise) server or we can use our local system to write a script.

Open Windows PowerShell ISE with Run as administrator.

If you get below error with respect to Machine policy then we just need to change the Execution policy of the system

Update Execution Policy as per below command but you need administrator access to run this command. Open the PowerShell editor as an administrator and then update the execution policy.

PS C:\> Set-ExecutionPolicy Unrestricted

As said earlier I am writing the scripts in my local system, so I need to keep all SharePoint DLL’s in local folder to provide the reference in the script.

You can copy the DLL’s from SharePoint server or download it from below URL .http://www.microsoft.com/en-ie/download/details.aspx?id=35585

I have created Library folder to keep all required.DLL files, which I’m referring in the code.

Below code will create Term sets based on provided XML inputs.

Script Code:


$0 = $MyInvocation.MyCommand.Definition

$dp0 = [System.IO.Path]::GetDirectoryName($0)

$xmlFilePath = $(“$dp0\Inputs.xml”)

$xmldata = [xml](Get-Content($xmlFilePath));

$username = “emailid@microsoft.com”

$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

# connect/authenticate to SharePoint Online and get ClientContext object..

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$credentials = New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

$clientContext.Credentials = $credentials

Add-Type -Path “C:\Library\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Library\Microsoft.SharePoint.Client.Runtime.dll”

Add-Type -Path “C:\Library\Microsoft.SharePoint.Client.Publishing.dll”

Add-Type -Path “C:\Library\Microsoft.SharePoint.Client.Taxonomy.dll”

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$credentials = New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

$clientContext.Credentials = $credentials

if (!$clientContext.ServerObjectIsNull.Value)


Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web




function createTerms([Microsoft.SharePoint.Client.Web] $web)


$TMS =[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($clientContext)





Write-Host “Connected to SharePoint Taxonomy” -ForegroundColor Green

#Retrieve Term Stores

$TermStores = $TMS.TermStores





Write-Host “Get SharePoint Taxonomy TermStores” -ForegroundColor Green

#Bind to Term Store

$TermStore = $TermStores[0]



#Get Groups

$Groups = $TermStore.Groups



#Get Site collection Group

$Group = $Groups[2]





Foreach ($TermMain in $xmldata.WebSite.TermMain)


$TermSet = $Group.CreateTermSet($xmldata.WebSite.TermMain.TermName,[System.Guid]::NewGuid().toString(),1033)

$TermSet.SetCustomProperty(“_Sys_Nav_IsNavigationTermSet”, “True”)

#$TermSet.SetCustomProperty(“_Sys_Nav_CustomSortOrder”, “True”)



Foreach ($TermSub1 in $TermMain.TermSet)


$TermSubSet1 = $TermSet.CreateTerm($TermSub1.Name,1033,[System.Guid]::NewGuid().toString())





Foreach ($TermSub2 in $TermSub1.Term)


$TermSubSet2 = $TermSubSet1.CreateTerm($TermSub2.Name,1033,[System.Guid]::NewGuid().toString())





Foreach ($TermSub3 in $TermSub2.TermSub)


$TermSubSet3 = $TermSubSet2.CreateTerm($TermSub3.Name,1033,[System.Guid]::NewGuid().toString())













createTerms $web

So here i am calling the createTerms function.

XML Input Values:

<?xml version=”1.0″ encoding=”utf-8″?>

<WebSite Url=”https://company.sharepoint.com/sites/Sample” >

<TermMain TermName=”Custom Top Navigation” GroupURL=”company.sharepoint.com” >

<TermSet Name=”Home” Url=”/” Order=”1″>


<TermSet Name=”Departments” Url=”/sites/Library” Order=”2″>

<Term Name=”Accounting” Url=”/sites/Accounting”>

<TermSub Name=”Accounting1″ Url=”/sites/Accounting/Accounting1″ />


<Term Name=”IT” Url=”/sites/IT” />

<Term Name=”Marketing” Url=”/sites/Marketing” />




Read some SharePoint 2013 tutorials:

I will post a few more scripts. Hope this will be useful.



Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →