IT SOS - Powershell error during new site deployment RRS feed

  • Question

  • Hello all.

    I am new to SharePoint (any version) and have built a new 2013 infrastructure for a customer. It consists of 2 x SharePoint Application Servers and 3 x SharePoint Web Front End Servers. The customer has had a third party develop a new application to be deployed onto the new infrastructure, but they have thrown it over the fence and stopped responding to e-mails. So I have the task of deploying the application which is mostly going ok for now but I have ran into an error that I am hoping is a simple fix. So apologies if I get some terminology wrong or sound like I don't know what I am doing, its because I don't.

    The script I am running is supposed to do the following:-

    Create a new Site Collection

    Activate web application level features

    Deploy pages with a number of web parts in place

    Create SharePoint Permissions

    Create SharePoint Groups

    I think most of it works except for the web part and page setup section. The error I see near the end of the script reports:-

    "You cannot call a method on a null-valued expression.

           $XTR = New-Object System.Xml.XmlTextReader($webPartDefinition.File.OpenBinaryStream())"

    I don't know what an expression is, but am assuming it is either "$XTR" or "$webPartDefinition".

    The section in the code looks like this:-

                    foreach($xWebPart in $xPage.WebParts.ChildNodes)
                        $wpCatalog = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog)
                        $webPartDefinition = $wpCatalog.GetItems() | Where-Object{$_.Name -eq $xWebPart.Name}

                        $XTR = New-Object System.Xml.XmlTextReader($webPartDefinition.File.OpenBinaryStream())

                        $wp = $wpm.ImportWebPart($XTR, [ref] $err)             
                        $wpm.AddWebPart($wp,$xWebPart.Zone, $xWebPart.Index)  

    After the script has exited by doing an IIS Reset, If I check the page it doesn't look like what it is supposed to look like.

    Does anyone have any advice on how to troubleshoot this problem. I was thinking it was related to "System.Xml.XmlTextReader" and that I am missing a software component to use that command.

    Any pointers will be greatly appreciated.

    If any more information is required I will do my best to get that.



    Friday, January 20, 2017 3:02 PM

All replies

  • Hi 2000DXP,

    Please check if the webPartDefinition is valid, generally, it is the web part xml code, it will contain all web part definition and properties. You can use Write-Host command to output it to check the detailed value.

    Also, please check the PowerShell demo below to add web part to a page:

    Add WebPart to Page using PowerShell


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Monday, January 23, 2017 6:36 AM
  • Hello, hope you are well.

    Thanks very much for the advice.

    I am on leave this week but will try out your suggestions when I am back.

    Thanks again,


    Monday, January 23, 2017 9:17 AM
  • Hi 2000DXP,

    Will wait for your updates.


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Wednesday, January 25, 2017 12:32 AM
  • Hi Jerry, hope you are well.

    Thanks for the advice you sent me, I have looked at the sample scripts that you referred to and I can see very similar bits of code that is in the script that I have been given. But the script that I am running has a more complexity that I am just not sure exactly what I am looking for to troubleshoot it.

    I have tried running the "write-host" command against the


    and it returns nothing. But that command is actually part of a loop. So really the problem I have is my lack of SharePoint experience and PowerShell scripts. It may be that it should return a blank.

    I am hoping the company who wrote and provided the script will respond soon as I am getting nowhere fast with it.



    Tuesday, January 31, 2017 1:28 PM