Navigate Up
Sign In
Supporters of Developer
Web

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

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
How to create Web Part instances on pages using PowerShell and SPLimitedWebPartManager

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

}
Categories:
No categories were selected

Comments

Waldek Mastykarz

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

Don't forget to dispose the SPLimitedWebPartManager instances as in http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_160

Posted 05-May-2009 by Waldek Mastykarz

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

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

Posted 11-Jan-2010 by
sg

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

help for new-object for custom webpart which is available in webpart gallery

** NOTE **** for this to work you have to edit the webpart xml and replace the GUID with the full type name

otherwise you will get "error importing web part".

For explanation see http://stackoverflow.com/questions/120928/sharepoint-error-cannot-import-web-part

**************************************************************************************************************************************

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null

function Add-CustomWebPart($SiteURL, $pageUrl)
{
 $site = new-object Microsoft.SharePoint.SPSite($SiteURL)
 $web=$site.OpenWeb()
 
 $webpartmanager=$web.GetLimitedWebPartManager( $pageUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
   
#supply XML reader with the path to your custom web part below 

[System.Xml.XmlTextReader]$oxmlReader = new-object System.Xml.XmlTextReader("C:\webparttests\sgtest.webpart");
 
 [System.Web.UI.WebControls.WebParts.WebPart] $oWebPart
 $oWebPart = new-object $webpartmanager.ImportWebPart($oxmlReader,[ref]$_.Exception.Message);
 $webpartmanager.AddWebPart($oWebPart, "Left", 0);
 $webpartmanager.Dispose;
 $web.Close()
 $site.Close()
}

Add-CustomWebPart " < your site url >" " <your page>"

Posted 20-May-2010 by sg
BGM

How to modify this script for a Page Viewer Web-Part

Salve! How can I modify this snippet to use a Page-Viewer Webpart? Or where can I look for the details?

Posted 06-May-2012 by BGM

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: