Pnp-powershell: Add-SPOWebPartToWebPartPage doesn't work with -xml flag

Created on 27 Apr 2016  Â·  5Comments  Â·  Source: pnp/PnP-PowerShell

When I try to pass well-formatted XML to Add-SPOWebPartToWebPartPage, it complains about the XML not being well formatted.

For example, I first export my existing web part into an XML file and then delete the web part (manually)

Get-SPOWebPartXml -ServerRelativePageUrl "it/default.aspx" -Identity "Team Documents" > $DataFolder\webpart.xml

Now, if I try to import the webpart from the file directly it works.

Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "it/default.aspx" -Path "$DataFolder\webpart.xm" -ZoneId "Left" -ZoneIndex "0"

However, reading the file into an XML object and then trying to import it fails

$wpXml = [xml](Get-Content "$DataFolder\webpart.xml")
Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "it/default.aspx" -Xml $wpXml -ZoneId "Left" -ZoneIndex "0"

I get the following error with the following error:
Add-SPOWebPartToWebPartPage : The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML.

Any thoughts?

Most helpful comment

the -xml parameter does not take an XML _object_ as input, but an XML string.

Try

$wpxml = Get-Content "$DataFolder\webpart.xml" -Raw

From Get-Help Add-SPOWebPartToWebPartPage

 -Xml <String>
        A string containing the XML for the webpart.

All 5 comments

file extension must be .webpart or .dwp please change an let us know

Hi B0tmaster,

In my actual code, I use .webpart files but that didn't work either.

Hello. Encountered the same problem using .xml - no success and same error message. Had to use .webpart and/or .dwp (never a problem so fare).

the -xml parameter does not take an XML _object_ as input, but an XML string.

Try

$wpxml = Get-Content "$DataFolder\webpart.xml" -Raw

From Get-Help Add-SPOWebPartToWebPartPage

 -Xml <String>
        A string containing the XML for the webpart.

Thanks. I will try that. Love Pnp. Great stuff!
7C.

Date: Thu, 5 May 2016 05:43:31 -0700
From: [email protected]
To: [email protected]
CC: [email protected]; [email protected]
Subject: Re: [OfficeDev/PnP-PowerShell] Add-SPOWebPartToWebPartPage doesn't work with -xml flag (#303)

the -xml parameter does not take an XML object as input, but an XML string.

Try

$wpxml = Get-Content "$DataFolder\webpart.xml" -Raw

From Get-Help Add-SPOWebPartToWebPartPage

-Xml
A string containing the XML for the webpart.

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub

    Virusfritt. www.avast.com       
Was this page helpful?
0 / 5 - 0 ratings