2

I am trying to add a custom webpart to a SharePoint site but I keep getting an error saying

it cannot deserialize the web part.

Any idea why? My google-fu is failing me.

 if ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PsSnapin "Microsoft.SharePoint.PowerShell"
    }

$customWebPartMainPage = get-spweb https://secure.site.com/siteName/

$webPartGallery = $customWebPartMainPage.Lists["Web Part Gallery"] #Retrieve Gallery of web parts
foreach($listItem in $webPartGallery.Items)
{
 if($listItem.Title -like "*Custom*")
 {
 $listItem.url
 $myListItem = $listItem
 }
}

$webpart= $webPartGallery.Items | ? { $_.Title -eq "Custom Web Part"} #Find 
specific Web Part


$web = get-spweb https://secure.site.com/siteName/SubSiteName/


$page=$web.GetFile("default.aspx");
$page.CheckOut();
$webpartmanager = $web.GetLimitedWebPartManager($page, 
[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$errorMsg = "";
$webpart
$xmlReader = New-Object 
System.Xml.XmlTextReader($webpart.File.OpenBinaryStream());
$xmlReader = New-Object 
System.Xml.XmlTextReader($webpart.File.OpenBinaryStream())
$webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")
$webpartmanager.AddWebPart($webpart, $webpartzone, $index);
$page.CheckIn('check in');
Write-Host("Webpart is added successfully") -ForegroundColor Green ;
3

Your Answer

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

Browse other questions tagged or ask your own question.