Saturday, 13 September 2014

Adding a web part on a page in Office 365 / SharePoint online team site (non publishing site) using CSOM in PowerShell


Hi All,

Moving forward to Office 365 development, today I’ll share how to add the Web Part on page of Office 365 / SharePoint Online team site using CSOM + PowerShell.

Previously I had written a post for adding Web Part on pages using PowerShell script. This PowerShell script is for on premises environment only. High level steps are very much similar J

Following are the steps and script


1.    To add the web part we will require web part definition file should be available on local file system so that we can import and add it using LimitedWebPartManager

 
2.    Now, consider we need to add WebPart on “MyWPPage.aspx” page from the Site Pages library of team site “MyWeb”

 
3.    Get the Microsoft.SharePoint.Client.ClientContext instance

 
4.    Get the Web instance

 
5.    Get the “Site Pages” library and find the your list item

 
6.    Get the Web instance

 
7.    Get the “Site Pages” library and find the your list item

 
8.    Get the file from list item

 
9.    Get LimitedWebPartManager instance

 
10. Get the web part XML from local web part file

 
11. Import the web part XML using LimitedWebPartManager

    
     12. Add the web part on page using LimitedWebPartManager


Complete Script:


#creating variable which we are using below

$page = $null

 #following variables can be parameterised


$WebPartFilePath = “c:\Site Feed.dwp”

$wpZoneID = “Zone1”
          $wpZoneOrder=1

#Password to connect the Office 365 site

$securePassword = ConvertTo-SecureString -String $Password -AsPlainText –Force

#instance of Client Context

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

 $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)
 

$site = $clientContext.Site

$web = $site.RootWeb


$clientContext.Load($web)

$clientContext.ExecuteQuery()

 
#List Collection
$lists = $web.Lists

$clientContext.Load($lists)

$clientContext.ExecuteQuery()


#Get the list

$list = $lists.GetByTitle(“Site Pages”)

$clientContext.Load($list)

$clientContext.ExecuteQuery()

 
#Creating CamlQuery instance; Getting all items

$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000, 'ID','Created','Modified')

 $items = $list.GetItems($query)

           $clientContext.Load($items)
           $clientContext.ExecuteQuery()
 
foreach($item in $items){

                   $file = $item.File

                   $clientContext.Load($file)

                   $clientContext.ExecuteQuery()

                   if($file.ServerRelativeUrl.Contains($FileNameWithExtention))

                   {
                             $page  = $file

                             break

                   }#if

          }#for each


$wpm  = $page.GetLimitedWebPartManager("Shared")

$clientContext.Load($wpm)
            $clientContext.ExecuteQuery()
 
$WebPartXml = get-content $WebPartFilePath

 
$wp = $wpm.ImportWebPart($WebPartXml)
 

$webPartadded = $wpm.AddWebPart($wp.WebPart,$wpZoneID,$wpZoneOrder)

$clientContext.Load($webPartadded)
 
            $clientContext.ExecuteQuery()

Thanks!

Enjoy reading J

Feel free to comment / feedback / suggestions /thoughts if any or if you have any query J

1 comment:

Ivan KLARMAN said...

Hello,
Thanks for your article, but i have a question.

We cannot do this with Publishing Site ?