Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:05:14+00:00 2026-06-14T05:05:14+00:00

I am writing a script that will allow us to build an entire SharePoint

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:05:15+00:00Added an answer on June 14, 2026 at 5:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that will allow a user to input a string that
I'm writing a script in Python that will allow the user to input a
I am writing a script that will allow me to search torrent sites. On
I'm writing a script that will loop through a range of numbers, build a
I'm writing a script that will constantly scan iTunes for new dialog boxes and
I am writing a script that will look in a custom reports directory, copy
I'm writing a script that will ping my ip range. Here's what I have
I'm writing a perl script that will access a MySQL database to determine whether
I am writing a shell script that will perform 2 tasks. One task will
I am writing a PowerShell script that will back up several folders from my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.