Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting Belgium – HostForLIFE.eu :: How to Add a Webpart to a SharePoint Page using a PowerShell Script?
One of the requirements of this article was to replace the current webpart from my SharePoint webpage that was bringing about numerous issues with execution. This web part was accessible in various content pages. So one of the choices accessible to us was to compose a PowerShell script to add another web part to various substance pages.
The article will describes how to add a webpart to a SharePoint page using a PowerShell script. To include the web part we will require a web part definition document to be accessible on the record framework so we can add it to the webpart exhibition, import it and include it utilizing the WebPartManager class.
Now, consider we have to add the webpart to the MyWPPage.aspx page from the page library of the web MyWeb. We have to get a distributed web instance from web instance and set the allow unsafe updates to true as follows:
1 2 |
$webURL = “http://MyWeb.com” $web = Get-SPWeb $webURL |
Get the distributed web occasion and set the allow unsafe update property to true.
1 2 3 |
[Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web); $allowunsafeupdates = $web.AllowUnsafeUpdates $web.AllowUnsafeUpdates = $true |
And now, get the pages library and find the list item with the following code:
1 2 3 4 5 6 |
$list = $web.Lists[$pagesLibrary] foreach($listItem in $list.Items){ if($listitem.URL.Contains("MyWPPage.aspx ") { $myListItem = $listItem break; } |
Get the document from listitem, watch that if this page is looked at to an other user or the same user and if the same user then publish and then check out the page.
1 2 3 4 5 6 7 8 9 10 11 12 |
$page = $web.GetFile($listitem.URL) if ($page.CheckOutStatus -ne "None") { #Check to ensure the page is checked out by same user, and if so, check it in if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin) { $page.CheckIn("Page checked in automatically by PowerShell script") Write-Output $page.Title"("$page.Name") has been checked in" } } if ($page.CheckOutStatus -eq "None"){ $page.CheckOut() #Get the webpart manager $webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) } |
From the local file system, get the file stream and import it using the webpart manager as follows:
1 2 3 4 5 6 7 8 9 10 11 |
$saveFolder = "C:\webpart\" $webpartfile = “MyWebPart.webpart” $fileDWP = $saveFolder + $webpartfile write-output "DWP File" $fileDWP #Getting the webpart gallery [Microsoft.SharePoint.SPList]$wpList = $web.Site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog) $fileStream = ([System.IO.FileInfo](Get-Item $fileDWP)).OpenRead() [Microsoft.SharePoint.SPFolder]$wpFolder = $wpList.RootFolder [Microsoft.SharePoint.SPFile]$wpFile = $wpFolder.Files.Add($webpartfile, $fileStream, $true) Write-host $wpFile [System.Xml.XmlReader]$xmlReader = [System.Xml.XmlReader]::Create($wpFile.OpenBinaryStream()) #Import the webpart $myCustomWP = $webpartmanager.ImportWebPart($xmlReader,[ref]$errorMsg) Write-Output "My custom WebPart" $myCustomWP.title #If this webpart is not available on page then add it if(! $webpartmanager.WebParts.Contains($myCustomWP)){ $webpartmanager.AddWebPart($myCustomWP, $wpZoneId, $wpOrder) } |
Check in the page & clean up the instances.
1 2 3 4 |
$page.CheckIn("Page checked in automatically by PowerShell script") $page.Publish("Page published,new webpart added") $web.AllowUnsafeUpdates = $allowunsafeupdates $xmlReader.Close() $pubWeb.Close() $web.Dispose() |
Hope it works for you!
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options
Print article | This entry was posted by Peter on March 12, 2015 at 3:47 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |