3

I wrote a Powershell script that creates SharePoint sites and pages based on an XML file to generate about 200 sites and more pages to build a specific structure.

The basic steps it does repeatedly (roughly below), given a newly created [Microsoft.SharePoint.SPWeb]$web:

    $pubSite = new-object Microsoft.SharePoint.Publishing.PublishingSite($web.Url)
    $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    $pagesLib = $pubWeb.GetPublishingPages()
    $contentType = $pubSite.ContentTypes["Name of Content Type"]
    $pageLayout = $pubSite.GetPageLayouts($contentType, $FALSE)
    $newPage = $pagesLib.Add("pagename.aspx", $pageLayout)

I've run this on all my Dev servers rebuilt several times and ALWAYS works. But when I tried to run the exact same script at one of our clients' with supposedly the same spec and build Windows Server 2k8 R2 servers, it always fails on the LAST LINE with:

Exception calling "Add" with "2" argument(s): "No parameterless constructor def ined for this object."

Case 1 above, was using the SharePoint 2010 Management Shell console (Run as Administrator) - for which it NEVER works

The strange thing is the EXACT SAME script, server and parameters WORK if I install Windows Powershell ISE on the clients' server and run the script in THAT.

Is there any significant difference running Powershell scripts between the Powershell ISE and the SharePoint 2010 Management shell? Possibly in terms of security, which might explain why it would NOT work using the SharePoint shell but would work using ISE

My guess is that the clients' server must have some manner of additional security setting from the organisation policy that wouldn't normally be deployed on Development boxes, that affects the privileges of running scripts between the 2 consoles.

Would appreciate ANY comments. Seriously at the end of my rope.

3 Answers 3

2

Check for null for variables, especially $pageLayout and $contentType

If ( $pageLayout )
{
    # use variable
}

I guess this is on SP2007 since you are not using cmdlets such as Get-SPWeb ?

1
  • Thanks. This is 2010. can you get a PublishingWeb object from Get-SPWeb? It's different, right? PublishingWeb vs SPWeb... Also I kind of already have an idea that it's the $pageLayout line becoming $NULL which is why it fails, but I'm wondering why it doesn't fail if I run the exact same script on ISE
    – zee
    Commented Jun 17, 2011 at 2:10
1

The one time I saw the exception "No parameterless constructor def ined for this object." was when I used comments in a pagelayout within a webpartzone. I really don't know if this is helping you, but could you please check this?

2
  • Thanks. I've got a comment above the zone but not within it... some have suggested that the tag <WebPartPages:webpartzone> was case-sensitive, i.e. it should be <WebPartPages:WebPartZone> which is why it breaks, but that seems bizarre..
    – zee
    Commented Jun 17, 2011 at 2:14
  • That was my issue as well but in SP2013 Commented Apr 18, 2014 at 22:11
0

Well, there's one thing I see that CAN go wrong:

$pageLayout = $pubSite.GetPageLayouts($contentType, $FALSE)

This can give you more than one page layout, since a content type can be associated with multiple page layouts...

Cheers Eau

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.