Click here to Skip to main content
       

Sharepoint

 
AnswerRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberihoecken8-Nov-12 1:00 
GeneralRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberbalaji101022-Nov-12 1:21 
AnswerRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberihoecken8-Nov-12 1:08 
GeneralRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberihoecken8-Nov-12 2:01 
GeneralRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberbalaji101011-Nov-12 23:58 
GeneralRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberihoecken12-Nov-12 0:17 
GeneralRe: How to make a sharepoint page accessible to all and settings custom themes based on User Pinmemberbalaji101012-Nov-12 0:41 
Question[SOLVED] Unable to add webpart to page from script, but successfull from command line using same code [modified] Pinmember_HawkeyeD5-Nov-12 9:41 
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.

modified 9-Nov-12 12:31pm.

AnswerRe: Unable to add webpart to page from script, but successfull from command line using same code Pinmemberihoecken6-Nov-12 2:05 
GeneralRe: Unable to add webpart to page from script, but successfull from command line using same code Pinmember_HawkeyeD6-Nov-12 4:04 
AnswerRe: Unable to add webpart to page from script, but successfull from command line using same code Pinmember_HawkeyeD9-Nov-12 6:30 
QuestionAdding users to the default members group PinmemberAptiva Dave1-Nov-12 8:17 
AnswerRe: Adding users to the default members group Pinmemberihoecken4-Nov-12 20:54 
GeneralRe: Adding users to the default members group Pinmemberbalaji10108-Nov-12 0:12 
AnswerRe: Adding users to the default members group [modified] Pinmemberihoecken8-Nov-12 0:21 
GeneralRe: Adding users to the default members group Pinmemberbalaji10108-Nov-12 0:33 
AnswerRe: Adding users to the default members group Pinmemberihoecken8-Nov-12 0:36 
QuestionSharePoint Standard 2007 to SharePoint 2007 Enterprise Pinmemberfmorales0931-Oct-12 10:24 
AnswerRe: SharePoint Standard 2007 to SharePoint 2007 Enterprise Pinmemberihoecken4-Nov-12 20:48 
QuestionNot been able to log into SharePoint site after deploying new WSP PinmemberNetMan201229-Oct-12 22:21 
AnswerRe: Not been able to log into SharePoint site after deploying new WSP Pinmemberihoecken29-Oct-12 23:06 
AnswerRe: Not been able to log into SharePoint site after deploying new WSP Pinmemberarunkumaryoyo31-Oct-12 19:52 
Questionshare document can detete by only who can uploaded not to others PinmemberHemadriT26-Oct-12 5:19 
AnswerRe: share document can detete by only who can uploaded not to others Pinmemberihoecken28-Oct-12 22:35 
GeneralRe: share document can detete by only who can uploaded not to others PinmemberHemadriT1-Nov-12 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 20 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid