Sunday, 25 December 2011

How to set/update webpart property using powershell script


Hello,
In our project we have situation, there we need to update the property of one webpart. Since our site is in production, we couldn’t recreate the site and this webpart is almost on every welcome page and lots of pages are created, we cannot manually change the property. So I decided to write the powershell script.
Following are the steps with powershell commands to update the webpart property
1.      Get the site
$site = Get-SPSite "SiteCollectionURL"
2.      Since we have to throughout the site collection, among all the web we will go through all the webs
$webcoll = $site.AllWebs
 #Step through each web in site collection
foreach ($web in $webcoll) {
3.       Now, we need to read welcome page of every web
#Get the root folder
$rootFolder = $web.RootFolder
#Get the welcome page
$welcomepage = $ rootFolder.WelcomePage
4.       Once we got the welcome page, get the file and checkout it
$file = $web.GetFile("$welcomepage")
$file.CheckOut()
5.       Now from welcome page we will get webpart manager
$wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
6.       Now we have webpartmanager from the page, we can traverse through all webparts and check for the webpart for which we need to set/update the property
foreach($webpart in $wpm.WebParts){
            if($webpart.MyUniqueProperty){
if(!$webpart. MyUniqueProperty){
$webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)
#More details for WebPartManager.SaveChanges()
                                    $wpm.SaveChanges($webpart)
}
}
}
7.       Check in and Publish the file - #more details SPFile.CheckIn()
                                $ file.CheckIn("Updated/Set property of webpart",1)
                        $ file.Publish("Updated/Set property of webpart")
8.       Finally dispose the site object
                $site.Dispose()           
Below the complete powershell script
$site = Get-SPSite "SiteCollectionURL"
$webcoll = $site.AllWebs
 #Step through each web in site collection
foreach ($web in $webcoll) {
#Get the root folder
$folder = $web.RootFolder
#Get the welcome page
$welcomepage = $folder.WelcomePage
$file = $web.GetFile("$welcomepage")
$file.CheckOut()
$wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
foreach($webpart in $wpm.WebParts){
            if($webpart.MyUniqueProperty){
if(!$webpart. MyUniqueProperty){
$webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)
                                    $wpm.SaveChanges($webpart)
}#First If
}#Second If
}#Foreach
$file.CheckIn("Updated/Set property of webpart",1)
            $file.Publish("Updated/Set property of webpart")
            $site.Dispose()           
}#foreach
I hope this will help you. Thanks. Feel free to discuss.





           


2 comments:

julian said...

Hi, I'm getting this exception 'Exception calling "SaveChanges" with "1" argument(s): "Object reference not set to an instance of an object.'
on this line $wpm.SaveChanges($webpart).
I tried everyway to update my webpart and I always end up getting this exception.

Prasham Sabadra said...

Hi,

Sorry for late response. I just viewed your comment. One quick thought is when you are getting the webpart manager as in above code

$wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

before the welcomepage check out or after that.

If you are getting webpart manager instance first and then checkout the page then might be the error will occur. Please checkout the page first and then get the webpart manager instance.

Hope this will help you.

Thanks!
Prasham