Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New-UsingBlock and BulkUpdateContext changes Context User
function New-UsingBlock {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[IDisposable]
$InputObject,
[Parameter(Mandatory = $true)]
[ScriptBlock]
$ScriptBlock
)
try {
$ScriptBlock.Invoke()
} finally {
if ($InputObject) {
$InputObject.Dispose()
}
}
}
Get-ChildItem -Path "master:\content\home" | % { Remove-Item -Path $_.ItemPath }
$watch = New-Object System.Diagnostics.Stopwatch
$watch.Start()
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
[Sitecore.Context]::User.Name
for($i = 1; $i -lt 10; $i++) {
$item = New-Item "master:/content/home/sample item $($i)" -Type "/sitecore/templates/Sample/Sample Item"
$item.Editing.BeginEdit()
$item["Title"] = "Sample Item $($i)"
$item["Text"] = "Sample Item $($i)"
$item.Editing.EndEdit() | Out-Null
}
[Sitecore.Context]::User.Name
}
$watch.Stop()
Write-Host $watch.Elapsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment