.NET King Logo Comments on: Active task shouldn't be deleted in SharePoint workflow
Skip Navigation Links
Home
Weblog
How to... (video)
Articles
About us
Contact us
0 Items in Video cart!

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.

using System;
using
System.Collections.Generic;
using
System.Text;
using
Microsoft.SharePoint;

namespace StopActiveTaskDelete
{
    //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
            base.ItemDeleting(properties);
            // 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:

using System;
using
System.Collections.Generic;
using
System.Text;
using
Microsoft.SharePoint; 

namespace RegsiterEventReceiver
{
    class Program
    {
        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"];
            myList.EventReceivers.Add(SPEventReceiverType.ItemDeleting,
                "StopActiveTaskDelete, Version=1.0.0.0, Culture=neutral" +
                //Replace this key with your own assembly publick key token
                ", PublicKeyToken=8a1a592e4ca85c4c",
                "StopActiveTaskDelete.CActiveTasksDelete");
            // Don't forget to update the list
            myList.Update();
            // Make sure you add this life to feel good after your
            // Solution is deployed successfully
            Console.WriteLine("Haha, done!");

        }
    }
}

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.
Cheers
Alireza


By: Craig  
Good post. I'd love to know why Microsoft didn't implement this as standard though. What use are workflow tasks if the user can just delete them??
URL:  
By: Biju  
Thanks for your reply, my requirement is as follows
1) I need to create eventhandler for the tasklist
2) the tasks should automatically based on any change in the ending date of its dependent task
3)In the same way if a new task is added between any two dependent tasks.
URL:  
By: Alireza  
Hi Biju,
Please clarify in detail and we can discuss it in detail.
URL:  
By: Biju  
Hi,

Am new to SharePoint, I have undergone the training on WSS and MOSS.

Can anyone please tell me how to Customize the exisitng Task List and Event Handler for Task Lists.

I want to update the tasklist based on the date and time of start and completion

Biju

biju.hsn@gmail.com
URL:  
Your name:
Email:
URL:
Comments:
 

 -