Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Created December 7, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellwest/43552f9c43581c6c71a9 to your computer and use it in GitHub Desktop.
Save michaellwest/43552f9c43581c6c71a9 to your computer and use it in GitHub Desktop.
The following script generates an xml file with data from items using Sitecore PowerShell Extensions 3.3.
# SPE 4.0 has upgraded the New-UsingBlock function to a cmdlet
Import-Function New-UsingBlock
$stringWriterDefinition = @"
using System.IO;
using System.Text;
public sealed class StringWriterWithEncoding : StringWriter
{
private readonly Encoding encoding;
public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}
public override Encoding Encoding
{
get { return encoding; }
}
}
"@
Add-Type -TypeDefinition $stringWriterDefinition
function New-Element {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[string]$Value
)
$data = $null
if($Value) {
$data = (New-Object System.Xml.Linq.XCData -ArgumentList $Value)
}
(New-Object System.Xml.Linq.XElement -ArgumentList $Name, $data)
}
$items = @(Get-Item -Path "master:\content\home") + @(Get-ChildItem -Path "master:\content\home")
[System.Xml.Linq.XDocument]$jobsXml = New-Object System.Xml.Linq.XDocument
$itemsElement = New-Element -Name "items"
$itemsElement.Add((New-Element -Name "timestamp" -Value ([datetime]::now)))
foreach($item in $items) {
$jobElement = New-Element -Name "item"
$jobElement.Add((New-Element -Name "title" -Value $item.Title))
$jobElement.Add((New-Element -Name "text" -Value $item.Text))
$itemsElement.Add($jobElement)
}
$jobsXml.Add($itemsElement)
New-UsingBlock ($writer = New-Object StringWriterWithEncoding -ArgumentList ([System.Text.Encoding]::UTF8)) {
$jobsXml.Save($writer)
$writer.ToString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment