Question

I want to get my Custom WebPart by name and add into a Page with PowerShell from (Web part gallery):

--> Edit Page --> Insert --> Web Part --> Custom

WebParts in image are added from Custom Feature(.wsp package) generated by Visual Studio

function AddWebPartsSolution($webAppURL){
    $userSolutionFileName = $Infrastructure.WebApplication.UserSolutionFile
    Add-SPSolution $scriptBase\Solution\$userSolutionFileName
    Install-SPSolution –Identity $userSolutionFileName –WebApplication $webAppURL –GACDeployment
}


function EnableWebPartsFeature($siteCollectionURL){
    #Get all Features -->  Get-SPFeature | Sort -Property DisplayName
    $featureName = $Infrastructure.WebApplication.UserSolutionName
    Enable-SPFeature -Identity featureName -Url $siteCollectionURL
}

Image:

enter image description here

I read this example, but PowerShell script get web part from local system. I need to get web part from image.

Where is the folder of Webparts from WebPart Gallery?

How to do this?

Was it helpful?

Solution

You can try and modify the below PS script:

#ensure that you use the site collection root url 
#if you use subsite url, will throw error since webpart gallery 
#exists only in the site collection root site

$web = Get-SPWeb "https://site-collection-url"

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

$publishingPage = $pubWeb.GetPublishingPage("https://site-collection-url/Pages/Test.aspx");

$publishingPage.CheckOut();

$list = $web.Lists["Web Part Gallery"]   

#use the webpart title or the .webpart file name

$wpl = $list.Items | where {$_.Title -eq "Your Webpart Name"}   

$xmlReader = New-Object System.Xml.XmlTextReader($wpl.File.OpenBinaryStream());   

$errorMsg = ""   

$wpManager = $publishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

$webPart = $wpManager.ImportWebPart($xmlReader, [ref]$errorMsg)   

#insert webpart into header zone at index 1

$wpManager.AddWebPart($webpart,"Header",1)  

$publishingPage.CheckIn("Added webpart");

$publishingPage.ListItem.File.Publish("Published");

$wpManager.Dispose()

$web.Dispose() 

Modified from:

Web Parts Connections via powershell

Add webpart to publishing page using PS

OTHER TIPS

Using the Microsoft PNP PowerShell Library, you can add WebParts to pages pretty easily.

For example to add from a webpart file:

Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -Path "c:\myfiles\listview.webpart" -ZoneId "Header" -ZoneIndex 1 

Or to add from XML where $webpart represents the XML:

Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -XML $webpart -ZoneId "Header" -ZoneIndex 1 

Adding to a wiki page has its own cmdlets but also can be added via path or XML:

Add-PnPWebPartToWikiPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -XML $webpart -Row 1 -Column 1

The site in the link above has examples on how to connect using the PNP library and how to get the WebPartXML if you need it. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top