Tuesday, 16 September 2014

Adding a web part page to Office 365 team site library using CSOM + PowerShell


Hi All,

Next step towards Office 365 development is uploading /adding a web part page in “SitePages” library of team site. The site is not publishing site so we couldn’t like upload the empty page and then apply PageLayout.


So we will require a local copy of page having appropriate zones. Example of page (default.aspx) structure (this page is just for reference J):

Fig 1. Sample Page
Following are the steps and script:


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


2.    Get the Web instance


3.    Get the “Site Pages” library


4.    Get the root folder and files collection


5.    For adding file, we need to create instance of Microsoft.SharePoint.Client.FileCreationInformation. Set the properties of this instance like FileUrl, FileContent etc.
 
      6.  Use the Add () method of files collection.

 Script:


#Variables which we are used below


             $LayoutPath = “c:\default.aspx”

$PagesLibraryTitle = “Site Pages”

$FileNameWithExtention = “default.aspx”

# All the above variables can be parameterised


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

#here assuming we are adding file to root web of site collection

$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($PagesLibraryTitle)

$clientContext.Load($list)

          $clientContext.ExecuteQuery()


#Files Collection

$files = $list.RootFolder.Files

$clientContext.Load($files)
          $clientContext.ExecuteQuery()


#instance of FileCreationInformation
[Microsoft.SharePoint.Client.FileCreationInformation]$fileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation

            #Get the file content
            $fileContent = Get-Content $LayoutPath
  
#convert the file content into Byte[] and set it to Content property of FileCreationInfo class

$fileCreationInfo.Content = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
            $fileCreationInfo.Url = $FileNameWithExtention #(default.aspx)


#add the file

$page = $files.Add($fileCreationInfo)
            $clientContext.ExecuteQuery()

Thanks!

Enjoy Reading :)

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


        

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