Thursday, 19 July 2012

Powershell to Add Web Part on Publishing Page

Ever came across a task of adding quite a few web parts(custom or out of the box) on a page? What if you have to do this for many pages across multiple environments? It becomes hassle especially when you have quite a few environments.

Well won't it be useful to have this chore scripted once and use it over and over again to avoid the hassle?

In recent implementations, I had to add quite a few custom web parts on publishing pages and then roll them out in other environments too. Initial thought was to manage it via UI, but after doing couple of times I got tired and ended up writing these scripts.

There is no out of the box command to achieve this, you have to use either client object model or server side APIs in powershell to do so.

Here are the steps to make use of these scripts in your project:
  1. Download sample config file from here add-webparts.xml
  2. Prepare XML file as per below guidelines
  3. Download web part definitions from your sharepoint dev environment or whereever their up to date definitions exist. e.g: web part gallery under site collection
  4. Execute scripts
Guidelines to prepare xml:
  1. set url under element
  2. add webpart element under webparts
  3. set Title, this value should be the same as web part definition title
  4. set LocalSrc, fully qualified path of web part definition file
  5. set Replace, flag to replace existing web part if existed 
  6. set ZoneName, name of zone on page where web part need to be added
  7. set ZoneIndex, index value of desired zone
  8. set WebPartID, un-used, leave it blank
  9. set DestinationPagePath, page where to add web part
  10. set Properties, add custom properties if required to be set
You can download full source code along with xml file from here

Hope this helps !

Majid

1 comment:

Ravi said...

thanks... helped me

Iconise ‘Add Picture’ control in PowerApps

Default look you get for ‘Add Picture’ control is not very appealing for end user and there is no OTB way of specifying icon for this con...