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)

        $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)

        $ErrorMessage = $_.Exception.Message    
        $returnVal = 1          

        $pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
        $page = $pweb.GetPublishingPage($pageURL)
        approvePage $page

        #close the xml reader
        if($xmlReader -ne $null)


        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.

  • Have you checked that the webpart you are trying to add has a <SafeControl> element in the package that deploys it? It's weird but it MIGHT be why.
    – Hugh Wood
    Commented Nov 5, 2012 at 21:51
  • It does and I have confirmed it loads into the web.config successfully and contains the correct public token Commented Nov 5, 2012 at 22:03

1 Answer 1


Ok, so I feel kind of stupid here since I didn't think about it myself, but here's the cause and I hope it helps someone else who comes along with the same issue as I did.

When adding a solution and activating said solution in Sharepoint, before you can access this solution in say adding a webpart to a page, you must first RESET IIS. So during my testing I did not add any code to reset the IIS on the application Sharepoint servers. The only reason I can think of that explains why it worked in the code block was that perhaps I had reset IIS inadvertently during my testing. I can't explain it otherwise. Stupid mistake and I have hung my head in shame, but there you go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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