
|
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 = ""
$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.
modified 9-Nov-12 12:31pm.
|
|
|
|

|
_HawkeyeD wrote: 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."
When something isn't registered safe you can add it by hand, but not by a function. I guess it should work without a function but I'm not sure.
The solution: Register the WebPart as Safe in webconfig:
<SafeControl Assembly="ResKitLibrary, Version=1.0.0.0, Culture=neutral,
PublicKey-Token=3fab337e00400dc5" Namespace="ResKitLibraryNamespace"
TypeName="*" Safe="True" />
I didn't test the script, but I think that's the solution.
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|

|
Hello,
Thank you for the reply. Actually, the safeControl entry is added to the web.config when I add the solution the webpart is contained in to SharePoint. This is a custom developed webpart and the entire solution contains Lists, Event Receivers and the webpart itself. When I add the solution to the SharePoint site and activate the Feature the SafeControl is loaded to the web.config automatically.
Thanks again.
|
|
|
|

|
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.
|
|
|
|