Reputation: 2655
I try to make a script which would update a webpart in sharepoint, I've found some example, but i have an issue with xml which i have to pass to web part.
Just for a note, the powershell which i use is 1.0.
$xmlDoc = New-Object xml;
$newXmlElement = $xmlDoc.CreateElement("HtmlContent");
$newXmlElement.InnerText="SomeValue";
So this piece of script where it breaks, i get an error saying:
Property 'InnerText' cannot be found on this object; make sure it exists and is settable.
I really don't know why it doesn't work, anyone has any idea?
I try to execute this lines in Windows Powershell comman line, and when i try to set innertext, it throws me this red error message.
Upvotes: 3
Views: 4302
Reputation: 11364
The error you get when you try to assign value to an existing element using dot notation is because dot notation accesses XmlElement
of the XmlNode
you are on. XmlElement
does not have InnerText
where XmlNode
does.
Example on how to create and assign value using InnerText
$xmlDoc = New-Object xml;
$newXmlElement = $xmlDoc.CreateNode("element", "HtmlContent", "")
$newXmlElement.InnerText = "SomeValue"
$secondXmlElement = $xmlDoc.CreateNode("element", "HtmlContentChild", "")
$secondXmlElement.InnerText = "NewValue"
$newXmlElement.AppendChild($secondXmlElement)
$xmlDoc.AppendChild($newXmlElement)
Example of loops and Node access
foreach($node in $nodes) {
if ($node.element -ne $null) {
$node.element.InnerText = "this will throw error" # generates the error
$node.element = "this is correct" # Correct way to add InnerText
}
else {
$elementToAdd = $xmlDoc.CreateNode("element", "element", "")
$elementToAdd.InnerText = "This is correct"
$node.AppendChild($elementToAdd)
}
}
Creates an XmlNode with the specified node type, Name, and NamespaceURI (NamespaceURI is null in your example).
Adds the specified node to the end of the list of child nodes, of this node
Upvotes: 0
Reputation: 2655
I finally found a way to do it, it seems that in PowerShell v1.0 the object System.Xml.XmlElement, doesn't have property like InnerText etc, so the way i did is following:
$xmlDoc=New-Object System.Xml.XmlDocument;
$xmlElement=$xmlDoc.CreateElement("HtmlElement");
$xmlText = $xmlDoc.CreateTextNode($cewpNewContent)
$xmlElement.AppendChild($xmlText);
I hope this could have been useful for someone else.
Upvotes: 3