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.

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:

cls

$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

$clientContext.Load($web)

$clientContext.ExecuteQuery()

}

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

{

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

$clientContext.Load($TMS)

$clientContext.ExecuteQuery()

if(!$TMS.ServerObjectIsNull.Value)

{

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

#Retrieve Term Stores

$TermStores = $TMS.TermStores

$clientContext.Load($TermStores)

$clientContext.ExecuteQuery()

if(!$TermStores.ServerObjectIsNull.Value)

{

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

#Bind to Term Store

$TermStore = $TermStores[0]

$clientContext.Load($TermStore)

$clientContext.ExecuteQuery()

#Get Groups

$Groups = $TermStore.Groups

$clientContext.Load($Groups)

$clientContext.ExecuteQuery()

#Get Site collection Group

$Group = $Groups[2]

$clientContext.Load($Group)

$clientContext.ExecuteQuery()

if(!$Group.ServerObjectIsNull.Value)

{

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”)

$clientContext.Load($TermSet)

$clientContext.ExecuteQuery()

Foreach ($TermSub1 in $TermMain.TermSet)

{

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

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

$TermSubSet1.SetLocalCustomProperty(“_Sys_Nav_SimpleLinkUrl”,$TermSub1.Url)

$clientContext.Load($TermSubSet1)

$clientContext.ExecuteQuery()

Foreach ($TermSub2 in $TermSub1.Term)

{

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

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

$TermSubSet2.SetLocalCustomProperty(“_Sys_Nav_SimpleLinkUrl”,$TermSub2.Url)

$clientContext.Load($TermSubSet2)

$clientContext.ExecuteQuery()

Foreach ($TermSub3 in $TermSub2.TermSub)

{

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

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

$TermSubSet3.SetLocalCustomProperty(“_Sys_Nav_SimpleLinkUrl”,$TermSub3.Url)

$clientContext.Load($TermSubSet3)

$clientContext.ExecuteQuery()

}

}

}

}

}

}

}

}

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>

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

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

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

</Term>

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

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

</TermSet>

</TermMain>

</WebSite>

Read some SharePoint 2013 tutorials:

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


Thanks,

Sambita


You May Also like the Following SharePoint Online 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 →