Thursday, November 30, 2006

Workflow with unpublishing


Nothing difficult, but I thought this might be interesting anyway.

So we need to implement a workflow which provides us with a possibility to schedule item deletion and finally unpublish it from the front end upon confirmation.

The key point is additional workflow state named "Deletion Pending" which is referred by the "Schedule for Deletion" command.It has two commands: "Confirm Deletion" and "Reject Deletion". The first command unpublishes item and the second returns it to the "Published" state.

The "Process" method that is invoked by the "Unpublish" workflow action looks like this:

public void Process(WorkflowPipelineArgs args) { Error.AssertObject(args.DataItem, "args.DataItem"); Item item = args.DataItem;

Database web = Factory.GetDatabase("web"); Database master = Factory.GetDatabase("master");

using (new Sitecore.SecurityModel.SecurityDisabler()) { DeleteItem(web, item); DeleteItem(master, item);

// RemoveLinks(item); LinkManager.UpdateLink(item); }

Sitecore.Context.ClientPage.SendMessage(this, String.Format("item:refresh(id={0})", item.ID)); }

private void DeleteItem(Database db, Item item) { if (db.Items[item.ID] != null) { db.Items[item.ID].Delete(); Log.Info(String.Format("Item {0} was deleted from the {1} database using the workflow command.", item.Paths.FullPath, db), this); } else { Log.Info(String.Format("Deletion for the item {0} failed for the {1} database. Item not found.", item.Paths.FullPath, db), this); } }

I've found two cons so far:

  1. multiple final states: "Published" and "Unpublished"
  2. the solution is not a substitute of the "Delete" which may be misleading.

Good luck.

1 comments:

Peter J. said...

Deleting the item i'd be a bit scared of if it's going to delete it both from web and master db, but it's a good example of customizing a workflow.

you could also add a Cancel action (when you just don't want to do all the steps and scrap what you're working on):

public class CancelAction
{
public void Cancel(WorkflowPipelineArgs args)
{
Item workflowItem = args.DataItem;
if (workflowItem.Versions.Count>1)
{
workflowItem.Versions.RemoveVersion();
}
}
}

Take care!

P.