The following powershell will add Custom visual webpart to a webpart page.
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$Url = "http://sp2013:5559" #Please remove trailing slash if any
$web = Get-SPWeb $Url
$web.AllowUnsafeUpdates=$true
function Add-WebPartToPage($pageUrl, $webpartzone,$index,$fileName)
{
$webPartGallery = $web.Lists["Web Part Gallery"]
Write-Host "Searching webpart $fileName in web part gallery" -ForegroundColor Yellow
if($webPartGallery -eq $null)
{
Write-Host("Unable to retrieve Webpartgallery");
}
$webpart = $null;
$webpart=$webPartGallery.Items | ? { $_.Title -eq $fileName}
if($webpart -eq $null) {
Write-Host("Unable to retrieve webpart: $fileName") -ForegroundColor Red
}
else
{
Write-Host("----------Adding Webpart--------")-ForegroundColor Yellow
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [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, $webpartzone, $index);
Write-Host($fileName +" Webpart is added successfully") -ForegroundColor Green ;
$wp=$webpartmanager.WebParts[1];
$wp.ChromeType="None";
$webpartmanager.SaveChanges($wp);
}
}
$SiteURL =$Url
#---------------Test Page----------------------------
$PageName="TestPage.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
Add-WebPartToPage "$SiteURL/SitePages/$PageName" "Header" 0 "Test Form"
$PageName="TestPage.aspx"
No comments:
Post a Comment