Ravendra's Blog

December 3, 2009

Sharepoint workflow on list item delete

Filed under: Uncategorized — Ravendra Mishra @ 6:04 pm

In order to trigger workflow on item delete you need to create event receivers and from event receiver call the workflow already attached with list. You may also pass parameters to the workflow.

FeatureReceiver.cs

using System;

using System.Collections.Generic;

using CMPM;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.Workflow;

namespace CMPM

{

    public class FeatureReceiver : SPFeatureReceiver

    {

        string assemblyName = “EventDeleteItemMPPWWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=42bd3e28d7726752”;

        public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            SPWebApplication webApp = null;

            if (properties.Feature.Parent is SPWeb)

            {

                SPWeb spWeb = properties.Feature.Parent as SPWeb;

                foreach (string strList in ActivityLists.Get())

                {

                    SPList list = spWeb.Lists[strList];                   

                    //Add date event receiver

                    string classNameDateHandler = “CMPM.DeleteEventHandler”;

                    list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, classNameDateHandler);                  

                }

                spWeb.Update();

            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            if (properties.Feature.Parent is SPWeb)

            {

                using (SPWeb web = (SPWeb)properties.Feature.Parent)

                {

                    foreach (string strList in ActivityLists.Get())

                    {

                        SPList List = web.Lists[strList];

                        foreach (SPEventReceiverDefinition listEventReceiverDefinition in List.EventReceivers)

                        {

                            if (listEventReceiverDefinition.Assembly == assemblyName && listEventReceiverDefinition.Type == SPEventReceiverType.ItemDeleted)

                            {

                                listEventReceiverDefinition.Delete();

                                break;

                            }

                        }

                    }

                    web.Update();

                }

            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) { /* no op */}

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { /* no op */}

        private List<SPWebConfigModification> CreateConfigModifications()

        {

            List<SPWebConfigModification> configModifications = new List<SPWebConfigModification>();

            //

            //  TODO: Add code to create web config modifications

            //

            return configModifications;

        }

    }

}

DeleteEventHandler.cs

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Workflow;

namespace CMPM

{

    public class DeleteEventHandler : SPItemEventReceiver

    {

        //public override void ItemDeleted(SPItemEventProperties properties)

        public override void ItemDeleting(SPItemEventProperties properties)

        {

            base.ItemDeleting(properties);

            string strListName = properties.ListTitle;

            SPWorkflowManager objWorkflowManager = null;

            SPWorkflowAssociationCollection objWorkflowAssociationCollection = null;

            using (SPWeb web = properties.OpenWeb())

            {

                SPListItem item = properties.ListItem;

                objWorkflowManager = web.Site.WorkflowManager; //item.Web.Site.WorkflowManager;

                objWorkflowAssociationCollection = web.Lists[strListName].WorkflowAssociations; //item.ParentList.WorkflowAssociations;

                foreach (SPWorkflowAssociation objWorkflowAssociation in objWorkflowAssociationCollection)

                {

                    //Find the GUID for the workflow association

                    if (objWorkflowAssociation.BaseId.ToString() == “dc891ef1-00e1-45fd-9774-528d00bf945d”)

                    {

                        MPPWWorkflowParameters objParams = new MPPWWorkflowParameters();

                        objParams.ListName = strListName;

                        objParams.IsDelete = true;

                        string strSerializedParams = objParams.getInitXmlString(objParams);

                        objWorkflowAssociation.AssociationData = strSerializedParams;

                        objWorkflowManager.StartWorkflow(item, objWorkflowAssociation,

                        objWorkflowAssociation.AssociationData, true);

                    }

                }

            }

        }

    }

}

WorkflowParameters.cs

using System;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml.Serialization;

namespace CMPM

{

    [Serializable()]

    public class MPPWWorkflowParameters

    {

        private string listName = string.Empty;

        private bool isDelete = false;

        public string ListName

        {

            get { return listName; }

            set { listName = value; }

        }

        public bool IsDelete

        {

            get { return isDelete; }

            set { isDelete = value; }

        }

        public string getInitXmlString(MPPWWorkflowParameters objParams)

        {

            MPPWWorkflowParameters parameters = new MPPWWorkflowParameters();

            parameters.ListName = objParams.ListName;

            parameters.IsDelete = objParams.IsDelete;

            using (MemoryStream stream = new MemoryStream())

            {

                XmlSerializer serializer = new XmlSerializer(typeof(MPPWWorkflowParameters));

                serializer.Serialize(stream, parameters);

                stream.Position = 0;

                byte[] bytes = new byte[stream.Length];

                stream.Read(bytes, 0, bytes.Length);

                return Encoding.UTF8.GetString(bytes);

            }

        }

    }

}

Advertisements

1 Comment »

  1. Could you write a detailed post of how you set up this project i.e. sequential workflow etc? And how you set up the folders within the TEMPLATE/Features folder with the feature.xml and eventreceiver.xml files

    Comment by Katie McCarron — February 15, 2013 @ 10:33 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: