Freitag, 20. Januar 2012

Powershell script how to add a webpart to a page

This script in no. 1 will add a webpart to a single page, no 2 will iterate through a site-collection and add the webpart to each sub-site/page. Please change the values in the %%- marked areas according to your settings/environment. Best practice would be to copy the code into PowerGui or any power-shell editor to see the code structured and the comments highlighted.

1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb

#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)


2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}

$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}

Keine Kommentare:

Kommentar veröffentlichen