Skip to content

Commit

Permalink
Nick's blog added to sample scripts: http://www.techphoria414.com/Blo…
Browse files Browse the repository at this point in the history
…g/2012/March/Change-Item-Templates-With-Sitecore-PowerShell

Read-Variable returned items are now wrapped with PowerShell extensions
  • Loading branch information
AdamNaj committed Sep 2, 2013
1 parent f63f6f0 commit 23f2a68
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
----item----
version: 1
id: {50456B81-0FB3-4705-BC61-E6FDD2CF98A3}
database: master
path: /sitecore/system/Modules/PowerShell/Script Library/Examples/Content Maintenance/Mass Change Item Templates
parent: {F25BF203-F58F-4F3C-B017-3ACD2F063479}
name: Mass Change Item Templates
master: {00000000-0000-0000-0000-000000000000}
template: {DD22F1B3-BD87-4DB2-9E7D-F7A496888D43}
templatekey: PowerShell Script

----version----
language: en
version: 1
revision: 1b5da6d3-8bcd-4f41-ba0f-ce0b04c88320

----field----
field: {B1A94FF0-6897-47C0-9C51-AA6ACB80B1F0}
name: Script
key: script
content-length: 1505

<#
Based on Blog by @Techphoria414 - Nick Wesselman
http://www.techphoria414.com/Blog/2012/March/Change-Item-Templates-With-Sitecore-PowerShell
#>

$sourceTemplate = get-item 'master:\templates\Sample\Sample Item'
$targetTemplate = get-item 'master:\templates\Sample\Sample Item'
$branch = get-item .
$result = Read-Variable -Parameters `
@{ Name = "branch"; Title="Branch to work on"; Root="/sitecore/"; Tooltip="Items you want to work on."}, `
@{ Name = "sourceTemplate"; Title="Current template"; Root="/sitecore/templates/"; Tooltip="Template you want to replace."}, `
@{ Name = "targetTemplate"; Title="New template"; Root="/sitecore/templates/"; Tooltip="Template you want to use."} `
-Description "This report will replace template on all items in the tree with another template of your choice" `
-Title "Branch Template Replace" -Width 500 -Height 400 `
-OkButtonName "Replace" -CancelButtonName "Cancel"

if($result -ne "ok")
{
Exit
}

$path = $branch.ProviderPath;
cd $branch.ProviderPath;
$targetTemplateItem = New-Object -TypeName "Sitecore.Data.Items.TemplateItem" -ArgumentList $targetTemplate
Write-Host "For branch '$($branch.ProviderPath)' changing templates from '$($sourceTemplate.Name)' to '$($targetTemplate.Name)'"
Get-ChildItem $path -recurse | Where-Object{$_.TemplateID -eq $sourceTemplate.ID} | ForEach-Object { $_.ChangeTemplate($targetTemplateItem); Write-Host "Changing $($_.ProviderPath)" };

Show-Result -Text -Width 800 -Height 700
Close-Window
----field----
field: {25BED78C-4957-4165-998A-CA1B52F67497}
name: __Created
key: __created
content-length: 15

20130902T111843
----field----
field: {8CDC337E-A112-42FB-BBB4-4143751E123F}
name: __Revision
key: __revision
content-length: 36

1b5da6d3-8bcd-4f41-ba0f-ce0b04c88320
----field----
field: {D9CF14B1-FA16-4BA6-9288-E8A174D4D522}
name: __Updated
key: __updated
content-length: 34

20130902T111843:635137175234032422
----field----
field: {BADD9CF9-53E0-4D0C-BCC0-2D784C282F6A}
name: __Updated by
key: __updated by
content-length: 14

sitecore\admin
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Management.Automation;
using Cognifide.PowerShell.PowerShellIntegrations.Commandlets.Interactive.Messages;
using Sitecore.Data.Items;
using Sitecore.Jobs.AsyncUI;

namespace Cognifide.PowerShell.PowerShellIntegrations.Commandlets.Interactive
Expand Down Expand Up @@ -85,7 +86,12 @@ protected override void ProcessRecord()
{
foreach (Hashtable result in results)
{
SessionState.PSVariable.Set((string) result["Name"], result["Value"]);
object resultValue = result["Value"];
if (resultValue is Item)
{
resultValue = ItemShellExtensions.GetPsObject(SessionState, resultValue as Item);
}
SessionState.PSVariable.Set((string)result["Name"], resultValue);
}
}
});
Expand Down

0 comments on commit 23f2a68

Please sign in to comment.