I am writing a script that will allow us to build an entire SharePoint environment, add solutions and implement web parts on particular pages from scratch. Nearly the entire script works except the last couple of lines which add the web parts to a page (of course). Here's the thing that is really weird. If I run the exact same code in a power-shell command line it works every time. The script that adds the web part to a page is called from a separate ps1 file that passes in the necessary information for the script to run. Here's the code:
function addWebPartToPage($siteURL, $pageURL, $webpartName, $listName, $solutionName, $destinationZone, $zoneIndex)
{
$web = Get-SPWeb $siteURL
$file = $web.GetFile($pageURL)
if($file.Exists)
{
try
{
checkOutPage($file)
$list = $web.Lists[$listName]
$wpManager = $web.GetLimitedWebPartManager($web.Url + $pageURL,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webPartListItem = $list.Items | where {$_.Title -eq $solutionName}
$xmlReader = New-Object System.Xml.XmlTextReader($webPartListItem.File.OpenBinaryStream());
$errorMsg = ""
#breaks here:
$webPart = $wpManager.ImportWebPart($xmlReader, [ref]$errorMsg)
$wpManager.AddWebPart($webpart,"$destinationZone",$zoneIndex)
}
catch
{
$ErrorMessage = $_.Exception.Message
$returnVal = 1
}
finally
{
checkInPage($file)
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$page = $pweb.GetPublishingPage($pageURL)
approvePage $page
#close the xml reader
if($xmlReader -ne $null)
{
$xmlReader.Close()
}
$wpManager.Dispose()
$web.Dispose()
[GC]::Collect()
checkIfFailed $returnVal
}
}
}
To prove the code actually worked, I opened a new PowerShell window with Admin rights (note the window that is calling the script itself is also using admin rights) and set the above variables to the values that would be passed in. I then copied and pasted my code block into the window and it ran successfully (WAT???).
So this has myself and a colleague completely stumped. Why does it work inside a script window as a code block, but won't run when I execute as a script. The error I receive is this:
An error occurred adding webpart Poll to the page Pages/WebpartTest.aspx. Error: Exception calling "ImportWebPart" with "2" argument(s): "A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe."
I would look to this being a config issue (safeExecute in the config etc) if it wasn't for the fact that it runs successfully via VS2010 and in a code block rather than through a script. All other functionalities of my script work including adding and activating solutions, creating Site Collections, etc.
Any help or input would be greatly appreciated.
Thank you.