How to create Web Part instances on pages using PowerShell and SPLimitedWebPartManager


Table of Contents

The example PowerShell scripts on this page will allow you to easily web parts to pages. This can be especially useful if you want to add a web part to multiple sites across your site collection without using the web user interface!

Content Editor Web Part

# Function:           Add-ContentEditorWebPart
# Description:        Adds the Content Editor Web Part to a page 
function Add-ContentEditorWebPart($SiteURL, $pageUrl, $webpartzone, $index, $title, $content)
{
	$site = new-object Microsoft.SharePoint.SPSite($SiteURL)
	$web=$site.OpenWeb()
	
	$webpartmanager=$web.GetLimitedWebPartManager($pageUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
	
	$webpart = new-object  Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
	$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
	$webpart.Title = $title
	
	$docXml = New-Object System.Xml.XmlDocument
	$contentXml = $docXml.CreateElement("Content");
	$contentXml.set_InnerText($content);
	$docXml.AppendChild($contentXml);
	
	$webpart.Content = $contentXml;
	$webpartmanager.AddWebPart($webpart, $webpartzone, $index);
	
	$web.Close()
	$site.Close()
}

This is a great example of how to customise the Person.aspx page that is used for the WSS/MOSS out of the box user profile page. You could then execute this script on every environment you work on.

Add-ContentEditorWebPart "http://moss:8888/" "http://win-0xqbnqw37uf:8888/Person.aspx" "MiddleRightZone" 0 "Admin" "<a href=`"http://www.sharepointdevwiki.com/`">sharepointdevwiki.com</a>"

Contact User Web Part

Source: Colin Byrne
Colin's example demonstrates adding the Web Part to a site with the Publishing Features enabled.

# Function:           Add-ContactWebPart
# Description:        Adds the Contact User Web Part to a publishing page
# Parameters:         SiteURL     -     Server relative URL of the Area 
#                     UserName -     UserName to show as the contact in domain\user format
#
# Requirements:       Needs to have the System.Web assembly loaded
# 
function Add-ContactWebPart($SiteURL, $UserName)
{
$comment = "Contact WebPart Added"

$site = new-object Microsoft.sharePoint.SPSite($SiteURL)

$web=$site.OpenWeb()

$user= $web.Users.get_item($UserName)

$pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

$defaultpage=$pubweb.GetPublishingPages()[$pubweb.DefaultPage]            

$defaultpage.CheckOut()

# Set Contact 
"Setting Contact on " + $pubweb.url + " to " + $user.Name
$defaultpage.set_Contact($user)
$defaultpage.Update()

$webpartmanager=$web.GetLimitedWebPartManager($defaultpage.Url,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart=new-object  Microsoft.SharePoint.Portal.WebControls.ContactFieldControl
$webpart.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
$webpart.Title="Page Contact"
$webpart.PicturePosition=[Microsoft.SharePoint.Portal.WebControls.PictureDirection]::Left
$webpart.IsDisplayJobTitle=$true
$webpart.IsDisplayPicture=$true


$webpartmanager.AddWebPart($webpart, "LeftColumnZone", 0);


" Checking in page"
$defaultpage.CheckIn($comment)

# Publish
if($defaultpage.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0)
{
        " Publishing"
        $defaultpage.listItem.File.Publish($comment)
}


# If moderation is being used handle the approval    
if ($defaultpage.listItem.ParentList.EnableModeration)
    {

        $modInformation = $defaultpage.listItem.ModerationInformation
        
        " Moderation on, Current Status: " + $modInformation.Status

        # Check for pending approval
        if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
        {
            " Approving"
            $defaultpage.ListItem.File.Approve($comment)
        }
            
    }
    

# Clean up
$pubweb.Close()
$web.Close()
$site.Close()

}

Labels

webparts webparts Delete
powershell powershell Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.


  1. May 05, 2009

    Waldek Mastykarz says:

    Don't forget to dispose the SPLimitedWebPartManager instances as in http://blogs...
  2. Jan 11

    Anonymous says:

    hi, i wouldl like to create instance of custom webpart which is not in GAC, can...

    hi,

    i wouldl like to create instance of custom webpart which is not in GAC, can anyone help for new-object for custom webpart which is available in webpart gallery.

    thanks

    Appaji


Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.