Convert Unversioned Media to Versioned. (Changing template for an Item )

In our sitecore application, there were a lot of unversioned media items. These are the media items derived from unversioned template. For Example, “/sitecore/templates/System/Media/Unversioned/Image” , “/sitecore/templates/System/Media/Unversioned/Jpeg”, “/sitecore/templates/System/Media/Unversioned/Pdf”.

We needed to convert the unversioned items to versioned items. Since the number of items were huge, sitecore powershell script was the best way to do it. Not just changing the template but we also had to copy the data when we change the template otherwise the data will be lost.

Below is the simple script came up with for this purpose.

#Script to convert Unversioned Items to Versioned Item
$rootItem = Get-Item master:"/sitecore/media library/Images";
#Unversioned Template ID (Source)
$unversionedImageTemplateID = "unversioned TemplateID";  
#Versioned Template ID  (Target)     
$VersionedImageTemplate = Get-Item "Versioned Template ID";  
$count = 0;

Get-ChildItem $rootItem.FullPath -recurse | 
Where-Object { $_.TemplateID -eq  $unversionedImageTemplateID} | 
ForEach-Object {
	$count++
	Write-Host $_.Paths.Path
    Set-ItemTemplate -Item $_ -TemplateItem $VersionedImageTemplate `
        -FieldsToCopy @{ Blob = "Blob"; 'File Path' = "File Path"; Alt = "Alt"; Width = "Width"; Height = "Height"; Extension = "Extension"; 'Mime Type' = "Mime Type"; Size = "Size"; Format = "Format"; Dimensions = "Dimensions"} 
}

Write-Host $count;
Write-Host "Success" -ForegroundColor Green

This script can be used to change any templates by giving the appropriate source and target template ID. Also by doing field mapping the field values can be copied when the template is changed.

Thank you!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: