1

I want to get web part XML from SharePoint Web Parts Gallery(not locally) for add webpart into a List page.

This is my code:

$spWeb = Get-SPWeb $webURL
$spSiteCollection = Get-SPWeb -Identity $spWeb.Site.Url

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spSiteCollection)
$pageName = "$pageName.aspx"
$list = $spWeb.Lists[$listName]
$publishingPage = $list.RootFolder.Files | where {$_.Name -eq $pageName}
$publishingPage.CheckOut();

$webPartslist = $spSiteCollection.Lists["Web Part Gallery"]   

#use the webpart title or the .webpart file name

$wpl = $webPartslist.Items | where {$_.Title -eq $webPartName}   

$webPartFile = spWeb.GetFile($wpl.Url)

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

$errorMsg = ""   

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

Result is:

enter image description here

  • 1
    Modify this line as $webPartFile = $spWeb.GetFile($wpl.File) and check – Gautam Sheth Jan 9 '18 at 14:14
  • 1
    Sorry modified comment, it should be $webPartFile = $spWeb.GetFile($wpl.File) , missed adding the $ – Gautam Sheth Jan 9 '18 at 14:16
  • Seen my updated question – Goshky Jan 9 '18 at 14:23
  • sharepoint.stackexchange.com/questions/233319/… Result from this accepted answer is the same. – Goshky Jan 9 '18 at 14:24
  • 1
    Sorry, i dont understand. The error is gone, right ? What's the issue ? not able to add webpart ? – Gautam Sheth Jan 9 '18 at 14:31
2

To add Web Part from SharePoint WebPart Gallery to publishing page, the following PowerShell for your reference.

$webURL="http://sp2013/sites/team"
$listName="Pages"
$pageName="Home"
$webPartName="SharePointProject1 - VisualWebPart1"

$spWeb = Get-SPWeb $webURL
$spSiteCollection = Get-SPWeb -Identity $spWeb.Site.Url

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spSiteCollection)
$pageName = "$pageName.aspx"

$list = $spWeb.Lists[$listName]
$publishingPage = $list.RootFolder.Files | where {$_.Name -eq $pageName}

$spWeb.AllowUnsafeUpdates = $true

if($publishingPage.ListItem.File.CheckedOutByUser -eq $null)
{           
     # Though it is newly created or already there, checkout.
     $publishingPage.CheckOut()
}

$webPartGallery = $spSiteCollection.Lists["Web Part Gallery"]   

#use the webpart title or the .webpart file name
$webpart=$webPartGallery.Items | ? { $_.Title -eq $webPartName}

if($webpart -eq $null) {
    Write-Host("Unable to retrieve webpart: $fileName") -ForegroundColor Red
}
else {  
    Write-Host("----------Adding Webpart--------")-ForegroundColor Yellow
    $webpartmanager=$publishingPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    $errorMsg = "";
    $xmlReader = New-Object System.Xml.XmlTextReader($webpart.File.OpenBinaryStream());
    $webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")    
    $webpartmanager.AddWebPart($webpart, "zone1", 1);
    Write-Host("Webpart is added successfully") -ForegroundColor Green ;
}
# Check in the Page with Comments
$publishingPage.CheckIn("Added webpart")
# Publish the Page With Comments
$publishingPage.Publish("Publish")
$spWeb.AllowUnsafeUpdates = $false

More information: http://sharepointpals.com/post/How-to-Add-AppParts-to-Publishing-Pages-in-SharePoint-2013-using-PowerShell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.