EnjoySharePoint YouTube Channel

Adding webparts programmatically Office 365 SharePoint 2013


Adding web parts programmatically to WebPart Page / Publishing page using CSOM and PowerShell Script for Office 365/SharePoint Online/SharePoint 2013. Below I will be sharing .ps script and XML input fields. In your project folder named as Web parts and the folder name will be specified XML file.

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

Create multiple < Webpart> tag provided by input values Path,ServerRelativeUrl, PageTitle, ZoneId, ZoneIndex.


Please find the sample .DWP file , which you can download and keep inside Web parts folder.

PS script code-

cls

$0 = $MyInvocation.MyCommand.Definition

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

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

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

$username = “samtest@microsoft.com”

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

$url = $xmldata.WebSite.Url

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

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

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

$clientContext.Credentials = $credentials

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

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

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

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

if (!$clientContext.ServerObjectIsNull.Value)

{

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

$web = $clientContext.Web

$clientContext.Load($web)

$clientContext.ExecuteQuery()

}

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

{

$pubWeb =[Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($clientContext,$web)

$clientContext.Load($pubWeb)

foreach($webpart in $xmldata.WebSite.Webparts.Webpart)

{

$pagesList = $web.Lists.GetByTitle($webpart.List)

$path=$webpart.Path

$filePath = $(“$dp0\$path”)

$webpartdata = [xml](Get-Content($filePath));

$wpxml=$webpartdata.OuterXml

$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery

$camlQuery.ViewXml = ‘<View><Query><Where><Eq><FieldRef Name=”FileLeafRef” /> `

<Value Type=”Text”>’+$webpart.PageTitle +'</Value></Eq></Where></Query></View>’

$items = $pagesList.GetItems($camlQuery)

$clientContext.Load($items)

$clientContext.ExecuteQuery()

$listItem = $items[0]

$clientContext.Load($listItem)

$clientContext.ExecuteQuery()

$file = $listItem.File

$file.CheckOut()

$clientContext.Load($file)

$clientContext.ExecuteQuery()

$WPManager =$web.GetFileByServerRelativeUrl($webpart.ServerRelativeUrl).GetLimitedWebPartManager(“Shared”)

$wpd = $WPManager.ImportWebPart($wpxml)

$file =$web.GetFileByServerRelativeUrl($webpart.ServerRelativeUrl).GetLimitedWebPartManager(“Shared”).AddWebPart($wpd.WebPart,$webpart.ZoneId,$webpart.ZoneIndex)

$listItem.Update()

$listItem.File.CheckIn(“”, [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)

$listItem.File.Publish(“”)

$clientContext.Load($listItem)

}

}

AddWebpart $web

XML Inputs-
<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<Webparts>
<Webpart Path=”Webparts\ContentWP.dwp” List=”Pages” ServerRelativeUrl=”/sites/test/Pages/SamplePage.aspx” PageTitle=”SamplePage.aspx” ZoneId=”Header” ZoneIndex=”1″></Webpart>
</Webparts>

<Folder Path=”Pages/Folder1″></Folder>
<Folder Path=”Pages/Folder2″></Folder>
<Folder Path=”Lists/ImageLibrary/Folder1″></Folder>
</Folders>
</WebSite>

Read some SharePoint tutorials:

Happy Scripting.
Thanks,
Sambita

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 →