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*")
 $myListItem = $listItem

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

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

$webpartmanager = $web.GetLimitedWebPartManager($page, 
$errorMsg = "";
$xmlReader = New-Object 
$xmlReader = New-Object 
$webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")
$webpartmanager.AddWebPart($webpart, $webpartzone, $index);
$page.CheckIn('check in');
Write-Host("Webpart is added successfully") -ForegroundColor Green ;

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.