Active task shouldn’t be deleted in SharePoint workflow (4/18/2008)
One of the typical problems in workflow development is task deletion. Usually users that interact with the task, have contribute permission to the task which means they can simply delete the tasks that are created as a part of workflow processing. Deleting a task can mess up the workflow and we may need to handle it as a part of the workflow. But how can we stop the users from deleting tasks? The solution is very simple. Item event handlers in MOSS/WSS 2007 can easily take care of this case. Item event handlers are events that are registered to a list and mapped to a dll that is already deployed to GAC. When an event (insert, delete and update) happens the event handler fires and execute the code that can do something in response or may even cancel the event trigger progression. This is exactly our scenario. We need to build an event handler that when a user tries to delete a task, it checks the task status. If the status is not "Completed" then in cancels the deletion and displays an error message. To implement this simply create a class library project in Visual Studio .NET and make a reference to Windows SharePoint Services Library. Then add the following code. Inline code description is included.
//Inhertit your class from SPItemEventReceiver
public class CActiveTasksDelete: SPItemEventReceiver
// Override the ItemDeleting function. You will find also ItemDeleted
// function that fires when the item is deleted. For your scenario we
// need ItemDeleting to stop the delete process before it is complete.
public override void ItemDeleting(SPItemEventProperties properties)
//Built in call to the initial definition
// Get reference to the current item being deleted
SPListItem myItem = properties.ListItem;
// Read the status field of the item being deleted
String taskStatus = myItem["Status"].ToString();
// Is the task status is Complete it is not active task any more
// Otherwise we cancel the deletion
if (taskStatus != "Completed")
// This is the message that we’d like to show to the user
// if he/she tries to delete an active task
properties.ErrorMessage = "This task is still in progress and cannot be deleted";
// This line actually cancels the delete
properties.Cancel = true;
Sign the assembly with a strong name key and deploy it to the global assembly cache. Now all you need to do is to introduce the this assembly to a task list in SharePoint. There are different ways of doing it, but the simplest way is using a simple consol application to do so.
Your script should look like this:
static void Main(string args)
// Get reference to the the site that containd your task list
SPSite mySite = new SPSite("http://sharepointSiteURL");
SPWeb myWeb = mySite.OpenWeb();
// Get the reference to your task list
SPList myList = myWeb.Lists["Tasks"];
"StopActiveTaskDelete, Version=188.8.131.52, Culture=neutral" +
//Replace this key with your own assembly publick key token
// Don’t forget to update the list
// Make sure you add this life to feel good after your
// Solution is deployed successfully
Now go to the SharePoint Site and try to delete an incomplete task. You will get this error page.
And this code will protect active tasks from deletion happily ever after.
(Source : Alireza)
Filed under: SharePoint