Vadim Tabakman

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

Posted by Vadim Tabakman Sunday, February 5, 2012 2:26:00 PM Categories: Custom Action Delete Item Event Handler Nintex Nintex Workflow Nintex Workflow SDK
Copyright Vadim Tabakman
Rate this Content 0 Votes

Comments

Friday, September 28, 2012 6:09:09 AM
MDD

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Very cool article.  I do need this for a sign up site, which was built into SP 2007.

Tuesday, March 25, 2014 2:55:33 PM
Fahad
Gravatar

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Hi Vadim, 

I had a quick question about this. Can you pass parameters to the workflow on start?

 

Regards,

Fahad

Tuesday, March 25, 2014 3:09:48 PM

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Hi Fahad,

yes you can.  You can add variable to a workflow and set a check box that makes the Start Variable. 

Then you can pass those to the workflow when it starts.

cheers,

Vadim

Friday, April 4, 2014 11:43:03 AM
Fahad
Gravatar

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Vadim, 

Is there a specific example that you can point me too? 

 

Kind Regards,

Fahad

Friday, April 4, 2014 11:57:54 AM

re: Nintex Workflow - Run a Workflow on Delete List Item Event

hi Fahad,

in the workflow designer page, go to Variables and create a new variable.

There should be a check box there that says Show on Start Form.

As soon as you do that and publish the workflow, go back to you other workflow, add a Start Workflow action, select your other workflow and you should be presented with parameters you can pass to it.

cheers,

Vadim

Friday, April 4, 2014 1:04:18 PM
Fahad
Gravatar

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Vadim,

In this example you are using C# code to start a workflow using WorkflowManager.StartWorkflow() Method. I was hoping to send parameters using the C# Code. Is there any way to do that?


Regards,

Fahad

Monday, April 7, 2014 3:01:43 PM

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Hi Fahad,

the StartWorkflow method is for starting SPD workflow.  If you want to start a Nintex Workflow, you need to use a web service call.  Information on the web service can be found in the Nintex Workflow SDK.

cheers,

Vadim

Wednesday, April 9, 2014 9:20:26 AM
Fahad
Gravatar

re: Nintex Workflow - Run a Workflow on Delete List Item Event

Hi Vadim,

Thanks for the reply. I will look into Nintex WF SDK.

 

Regards,
Fahad

You must sign in to this site to post comments.
Already Registered?
Sign In
Not Yet Registered?
Register

Statistics

  • Entries (279)
  • Comments (1769)

Categories