This time, I was supposed to add a webpart to SharePoint Online page programmatically.
Let’s share the code to add a
Content Editor webpart to a SharePoint page:
While I was developing this program, I found a problem with the format of XML. Specifically this problem:
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.
$wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)
Using PowerShell is really important consider the type of the variables we are using, in my case I was trying to
ImportWebPart using a PowerShell variable called
$WebPartXml, but the real XML object (as string) that
ImportWebPart function expects is
Lot of thanks to my friend Benja for helping on that!