Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cwparsons/7e90b512b1ad6bd183e60fb8a445fa47 to your computer and use it in GitHub Desktop.
Save cwparsons/7e90b512b1ad6bd183e60fb8a445fa47 to your computer and use it in GitHub Desktop.
$contextItem = Get-Item .
$importFilePath = Receive-File -Path "$($SitecoreDataFolder)\importredirects" -Description "Creates simple redirect items from a CSV file with columns Name,Path,Target" -OkButtonName "Import"
if($importFilePath -eq "cancel") {
Exit
}
$csv = Import-Csv $importFilePath
$csv | % {
$name = $_.Name
$path = $_.Path
$target = $_.Target
if ($target -notmatch "http") {
$targetItem = Get-Item -Path $target
}
$redirect = Get-Item -Path master: -Query ("fast:" + $contextItem.Paths.Path + "/*[@Path='$path']")
if($redirect) {
Write-Host "Updating " -NoNewLine -ForegroundColor Yellow
Write-Host "$($name): $path -> $target" -NoNewLine
} else {
Write-Host "Creating " -NoNewLine -ForegroundColor Yellow
Write-Host "$($name): $path -> $target" -NoNewLine
$redirect = New-Item -Path $contextItem.Paths.FullPath -Name $name -ItemType "/sitecore/system/Modules/Url Rewrite/Templates/Inbound/Simple Redirect"
}
$redirect.Editing.BeginEdit()
$redirect.Name = $name
$redirect.Path = $path
if ($target -match "http") {
[Sitecore.Data.Fields.LinkField]$targetField = $redirect.Fields["Target"]
$targetField.LinkType = "external"
$targetField.Url = $target
} else {
$redirect.Target = $targetItem
}
$redirect["__Workflow state"] = "{7C920CDE-F8EC-44BA-9DCB-47B80329A50F}"
if($redirect.Editing.EndEdit()) {
Write-Host " Done." -ForegroundColor Green
} else {
Write-Host " Failed." -ForegroundColor Red
}
}
Show-Result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment