Nintex Workflow

Nintex Workflow – Run a Workflow on Delete List Item Event

I’m sure a lot of you have wanted to have a workflow run when someone decides to delete an item out of a List. Ok, maybe not a lot of you, but some. Ok, atleast a couple, because I’ve seen the questions on Nintex Connect.

So the only way to do this, would be to have a SharePoint Event Handler. I won’t go through the process of creating one of those, as you can follow this link : How to: Create an Event Handler Feature

Now kicks in the thinking about what we really want to do, and how efficiently we can do it.

I don’t really want to create a new Event Handler for every list where there will be a delete event. I’d also like for there to be nice single location where this can be configured.

The Design

I’m going to add my Delete Event Handler into a SharePoint feature. This can therefore be activated at the Site level. Every time someone performs a delete action on a SharePoint List Item in site where this is activated, the event handler will be executed.

The Event Handler is going to expect a specific List to exist on the site it is running on. I chose a cryptic name for the list – “Delete Event Handler Workflows”. This list will contain (List,Workflow) pairs. When the Event Handler kicks in, it will query this list for any items that match the current list the Event Handler is looking at. If it does, it gets the Workflow name and then starts that workflow on the item that is trying to be deleted.

Once starting the workflow, it will go into a loop and every 10 second, it will check to see if the workflow has finished.

As the Event Handler will wait, and therefore make the deletion wait, you probably want to make the workflows the Event Handler starts, fairly quick.

What does the source look like?

What I’ve created is a very straightforward piece of code. It can really be enhanced greatly. This is what the main part of the Event Handler source looks like.
 

            SPWorkflowManager workflowManager = properties.ListItem.Web.Site.WorkflowManager;
            SPWorkflowAssociationCollection waColl = properties.ListItem.ParentList.WorkflowAssociations;

            List workflowNamesToRun = new List();
            string deleteHandlerListName = "Delete Event Handler Workflows";
            string currentListName = properties.ListItem.ParentList.Title;

            try
            {
                SPList deleteHandlerList = properties.ListItem.Web.Lists[deleteHandlerListName];

                SPQuery oQuery = new SPQuery();
                oQuery.Query = "" +
                    "" + currentListName + "";
                SPListItemCollection collListItems = deleteHandlerList.GetItems(oQuery);

                foreach (SPListItem oListItem in collListItems)
                {
                    workflowNamesToRun.Add((string)oListItem["Workflow Name"]);
                }

                if (workflowNamesToRun.Count > 0)
                {
                    List waToStart = new List();

                    foreach (SPWorkflowAssociation wfAssoc in waColl)
                    {
                        if (workflowNamesToRun.Contains(wfAssoc.Name))
                            waToStart.Add(wfAssoc);
                    }
                    foreach (SPWorkflowAssociation wfAssociation in waToStart)
                    {
                        try
                        {
                            properties.ListItem.Web.Site.AllowUnsafeUpdates = true;
                            SPWorkflow workflow = workflowManager.StartWorkflow(properties.ListItem, wfAssociation, wfAssociation.AssociationData);

                            while (workflow.IsCompleted != true)
                            {
                                System.Threading.Thread.Sleep(10000);
                            }

                            Console.WriteLine("Complete...");
                            //properties.Cancel = true;
                            //properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported." + Environment.NewLine + names;
                        }
                        catch (Exception ex)
                        {
                            properties.Cancel = true;
                            properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported." + Environment.NewLine + ex.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // the Delete Event Handler Workflows list does not exist
            }

One suggestion for enhancing this, would be to somehow have your workflow do something, that tells the Event Handler to cancel the deletion.



Note

This Event Handler currently only works for SharePoint Lists. It is not coded to work with Document Libraries, Tasks etc. So this might be something to think about when you are creating an Event Handler.



Deployment

The SharePoint solution can be installed using the STSADM command line tool :

stsadm -o AddSolution -filename VTNWDeleteEventHandler.wsp
stsadm -o DeploySolution -name VTNWDeleteEventHandler.wsp -immediate -allowgacdeployment
stsadm -o installfeature -filename VTNWDeleteEventHandler\Feature.xml
iisreset

To activate the feature at the SharePoint Site level, you can either go to Site Settings and click on Site Features, and enable the feature there :

Or, you can use the STSADM tool :

stsadm -o activatefeature -filename VTNWDeleteEventHandler\Feature.xml -url 

And to deactivate the feature :

stsadm -o deactivatefeature -filename VTNWDeleteEventHandler\Feature.xml -url 

The SharePoint solution can be removed using the STSADM command line tool :

stsadm -o uninstallfeature -filename VTNWDeleteEventHandler\Feature.xml
stsadm -o RetractSolution -name VTNWDeleteEventHandler.wsp -immediate
stsadm -o DeleteSolution -name VTNWDeleteEventHandler.wsp

Usage

To test this out, I work in a list named “TestDeleteHandler”. I created 2 Nintex Workflows that will copy an item to another list named “Archived Items”.

First workflow looks like this :

To test this Event Handler, we need to first create an item in the “TestDeleteHandler” list. I

create the following item with a title field of “Seven”. I do this with a Nintex Workflow (because it’s a lot faster than doing it in SharePoint Designer).

Then we delete this item and our Event Handler should kick in, and start off 2 workflows. One will do a direct copy of the item to the “Archived Items” list, and the other with do the same, except with a different Title.

Now that we know it works, we’re ready to use this in a more realistic scenario, such a doing backups of items in case the deletion is an accident. I’m ignoring the existence of the Recycle Bin :).



Useful Downloads

VTNWDeleteEventHandler.wsp

Leave a Reply