Saturday, March 11, 2006

Items that are Done but not published


private void Page_Load(object sender, System.EventArgs e) { Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Items.Item rootItem = master.Items.GetItem("/sitecore/content/home/"); // the ID of the "Simple" workflow string workflowID = "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}"; // checking all children of the rootItem foreach (Sitecore.Data.Items.Item item in rootItem.Children) { // getting the current workflow state of the item string stateName = GetState(item, master, workflowID).DisplayName; // if the item is in the "Done" state, outputting its name if (stateName == "Done") { Response.Write(item.Name); } } } private Sitecore.Workflows.WorkflowState GetState(Sitecore.Data.Items.Item item, Sitecore.Data.Database database, string workflowID) { // getting the workflow provider for the master database Sitecore.Workflows.IWorkflowProvider provider = database.WorkflowProvider; // getting the Simple workflow through the IWorkflow interface Sitecore.Workflows.IWorkflow iWorkflow = provider.GetWorkflow(workflowID); return iWorkflow.GetState(item); }

4 comments:

Anonymous said...

Great to see you posting again. I have a suggestion on how to improve that:

You can use the IWorkflow.GetItems(string stateID) method to get all the items in the given state. So instead of iterating through the entire content tree you can enumerate all 'final' workflow states and then get all items in those state using IWorkflow.GetItems().

What is good about it, is that it is that IWorkflow.GetItems() is directly supported by the DataProvider.GetItemsInWorkflowState() method (it is required to have things like workbox run in reasonable amount of time) - it should be pretty fast.

Anonymous said...

And another idea is to see if the state is final (the field is named 'Final', 'Is Final' or something like that) instead of doing a name check.

Anonymous said...

The title of this document is "Items that are done but not published". I think this is misleading as there are no check if the item is published.

As far as I can read, this piece of code lists all items that are in a state named "Done" and not really considering publishing.

Anonymous said...

Good article though!

And nice to see you (finally) posting again.

Huh, makes me think that I should do likewise :-)