Thursday, July 05, 2007

Tutorial: Restore from Archive functionality

this post applies to Sitecore v5.3.1

the archive database in sitecore is a feature that's easily missed and not taken full advantage of. how it can work best for you and your solution i'll leave to you to decide, but there is one thing you might get frustrated at, and that's the fact that there's no real Restore functionality available from the content editor.. well, that is, until now :)



Follow these steps and you'll have your very own Restore Now functionality for the archive.

we're gonna:
  • Create a new command that will handle the restore functionality
  • Add the command to the config
  • Add a button to the Archive menu that will allow us to trigger the new command

step 1: creating the code for the command

Create a new C# class and name it ArchiveRestoreCommand

add this code:

using System;
using System.Collections.Generic;
using System.Text;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Archiving;
using Sitecore.Shell;
using Sitecore.Shell.Framework;
using Sitecore.Shell.Framework.Commands;
using Sitecore.SecurityModel;
using Sitecore.Tasks;
namespace usoniandream.extensions
{
[Serializable]
public class
ArchiveRestoreCommand : Command
{
public
ArchiveRestoreCommand()
{
}
public override void Execute(CommandContext context)
{
// make sure we only run this if we're in the archive database..
if
(Sitecore.Context.ContentDatabase==Sitecore.Configuration.Factory.GetDatabase("archive"))
{
//
disable security to allow restoration of item..
using (SecurityDisabler disabler = new SecurityDisabler())
{
// find archive item submitted
ArchiveItem archiveItem =
Sitecore.Context.ContentDatabase.GetItem(ID.Parse(context.Parameters[0]));
if
(archiveItem != null)
{
// reassure the archive is correct..
Sitecore.Data.Archiving.Archive archive = archiveItem.Archive;
if (archive!=null)
{
// restore item from the archive without removing it from the archive.. (change 'false' to 'true' to remove it)
archive.RestoreItem(archiveItem, false);
// broadcast success information..
Context.ClientPage.ClientResponse.Alert("Item restored.");
}
}
}
}
else
{
// broadcast informative error for not being in the correct database..
Context.ClientPage.ClientResponse.Alert("Restoring an item can only be done from the archive database.");
}
}
public override CommandState QueryState(CommandContext context)
{
// only show this action if we're actually in the archive database
if (Sitecore.Context.Database.Name!="archive")
{
return CommandState.Hidden;
}
return base.QueryState(context);
}
protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
{
}
}
}


Step 2: Add the command

compile the newly created code and open the App_Config\commands.config file and add the following line to it:

<command name="item:restore" type="usoniandream.extensions.ArchiveRestoreCommand,usoniandream.extensions"/>

Step 3: Customizing the Sitecore archive menu

so, now that the above is all taken care of, go into Sitecore and switch to the Core database, then:

  • Navigate to Sitecore\Content\Globals\Archives Menu
  • Duplicate the Archive Now item and name the new one Restore Now
  • Change the appropriate texts.. nice icon would be Network\16x16\server_out.png
  • Change the click to item:restore(id=$Target)
  • Save!

Hope you'll find this useful, next archive addition will hopefully be more useful, like a Preview or something..

Take care,

P.

2 comments:

Kim Hornung said...

Hi Peter,

Excellent - thank you for providing this tutorial.

If you wish to only show the button when running in the archive database, you could simply change the QueryState method:

public override CommandState QueryState(CommandContext context)
{
if (Sitecore.Context.Database.Name!="archive") {
return CommandState.Hidden;
}
return base.QueryState(context);
}

Best Regards,
Kim Hornung

Unknown said...

Hey Kim!

thanks, that's indeed just what i wanted :)

P.