Skip to content

Instantly share code, notes, and snippets.

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/1b7fe5a1a45e9fbf317f to your computer and use it in GitHub Desktop.
Save michaellwest/1b7fe5a1a45e9fbf317f to your computer and use it in GitHub Desktop.
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