Recently we have seen how to Delete Web parts in SharePoint 2010 and 2013 with Windows PowerShell. A logical next step would be to add Web Parts also with Windows PowerShell. So we would have two scripts with which we may replace a given Web element by another. An example of use? Personally I have used these two scripts on several occasions to replace (as I said, remove a Web Part, and then add another in its place) Web Parts from welcome pages from all sites in a given site collection. Although knowing what the scripts do, you can find many other uses.

Well, made the introduction, let’s go to start. This would be the core of the script that would allow us to add Web Parts to a SharePoint Web page by using Windows PowerShell. First, we take a look at the code, and then we analyze it:

#Get the Welcome Page URL of the current site.
$welcomePageUrl = $oWeb.ServerRelativeUrl + "/" + $oWeb.RootFolder.WelcomePage;

#Get the page.
$page = $oWeb.GetFile($welcomePageUrl);

#First, we make the checkout to the page.
if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout -or
   ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Published -and $page.CheckedOutByUser -ne $null)) {
	
	#Undo-CheckOut in case the page were already cheked-out	
	$page.UndoCheckOut(); 
	$page.Update();
	
	$page.CheckOut();
	$page.Update();			
}

#Get the WebPartManager from the page.
$spWpManager = $oWeb.GetLimitedWebPartManager($welcomePageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);		

#Adding a Content Editor Web Part.
$wp = new-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart;
$wp.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::None;
$wp.Title="Content Editor desde PowerShell";
$content = "<div>El cuerpo en HTML del CEWP</div>";
$xmlDoc = New-Object System.Xml.XmlDocument;		
$xmlElement = $xmlDoc.CreateElement("Content");
$xmlElement.Innertext = $content;		
$wp.Content = $xmlElement;
$spWpManager.AddWebPart($wp, "Zone A", 1);		
$wp.Dispose();

# Function that approves and publish the page.
PublishPage($page);
$oWeb.Update();

As we see, first we need to get the page in question where we want to add Web Parts. Next, we need to check out the page for editing. It could be that the page were already checked out by another user. In this case the behavior of the script is override the check out, but perhaps this is not the desired behavior. It is up to the user to avoid do an “Undocheckout” or leave it as is.

Now that the is page ready to be edited, we get the WebPartManager object, which will allow us to “play” with the Web Parts. In the example we add a Content Editor Web Part. Depending of the Web Part that you add, we will have to set some properties or others. One way to figure out what properties we need to establish is to access a page that already contains the desired Web Part and through Windows Powershell get the WebPartManager, get the Web Part, and then review all of its properties. So, in the case of Content Editor Web Part, the most important property is “Content”, which will allow us to insert the content in HTML format. This property only accepts XmlElement objects, hence the “tricks” with XmlDocument carried out above.

After configuring the Content Editor Web Part, all that remains is to add it using the AddWebPart method of the WebPartManager object. We have to pass to the method the WebPart object itself, the Web Part Zone where you want to add the Web Part and the Index. Index is used in case you already have more than one Web Part in the same Web Part Zone, indicating in what order should appear. And with this we would have everything.

I hope this have been helpful. Until next time 🙂

 

Cristian Zaragoza

Cristian has more than three years of experience as SharePoint Developer. Cristian is a former SolidQ team member.