Skip to content

Instantly share code, notes, and snippets.

@sohelsd
Last active August 29, 2015 14:13
Show Gist options
  • Save sohelsd/076b94cfca5c0e81cc20 to your computer and use it in GitHub Desktop.
Save sohelsd/076b94cfca5c0e81cc20 to your computer and use it in GitHub Desktop.
Sitecore Unversioned to Versioned media items
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Shell.Framework.Commands;
namespace MySite.Website.Utilities
{
public class VersionButton: Command
{
Sitecore.Data.Database Master = Sitecore.Configuration.Factory.GetDatabase("master");
public override void Execute(CommandContext context)
{
if (context.Items.Length == 0) return;
Item item = context.Items[0];
var templateItem = GetItem(item.TemplateID);
var templatePath = templateItem.Paths.FullPath;
var newPath = templatePath.Replace("Unversioned", "Versioned");
var newTemplateId = GetItem(newPath);
var newTemplate = Master.GetTemplate(newTemplateId.ID);
using (new Sitecore.SecurityModel.SecurityDisabler())
{
item.Editing.BeginEdit();
item.ChangeTemplate(newTemplate);
item.Editing.AcceptChanges();
}
}
public override CommandState QueryState(CommandContext context)
{
foreach (Item item in context.Items)
{
var templateItem = GetItem(item.TemplateID);
if (templateItem != null &&!templateItem.Paths.FullPath.Contains("Unversioned"))
{
return CommandState.Hidden;
}
}
return base.QueryState(context);
}
private Item GetItem(string path)
{
return Master.GetItem(path);
}
private Item GetItem(ID itemId)
{
return Master.GetItem(itemId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment