Monday, November 25, 2013

How to attach an event handler to a SharePoint List using C#? a simple reusable method for attaching an event handler

Creation of event handler requires creation of an event receiver class in SharePoint .  For this we would  need to create a feature receiver from the already built in template of visual studio.  Once the receiver is created we can use the following code to attach the event handler to any list.


The following is the code to attach the event handler to the list

Reciever.cs

 private void AddHandler(SPList objList, string strRecieverName, int iseqno, SPEventReceiverType eventType)
        {
            try
            {
                SPEventReceiverDefinitionCollection objEventDefColl = objList.EventReceivers;
                ////Set the values for the Definition object 
                Assembly objAssembly = Assembly.GetExecutingAssembly();
                string strAssemblyName = objAssembly.FullName;
               
// MyCustomClass.cs" is the Name of the class in which the logic is writen to handle various events like adding,updating,deleting
                 string strClassName = "MyCustomClass.cs";
      
                string strReceiverName = strRecieverName;
                string strDefData = "Data";
                int iSequenceNo = iseqno;
                ////Create the Definition object 
                SPEventReceiverDefinition oEventDef = objEventDefColl.Add();
                ////Set the properties
                oEventDef.Name = strReceiverName;
                oEventDef.Assembly = strAssemblyName;
                oEventDef.Class = strClassName;
                oEventDef.Data = strDefData;
                oEventDef.SequenceNumber = iSequenceNo;
                oEventDef.Type = eventType;
                oEventDef.Update();
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }
        }


---------------------------------------------------------------------------------------------------------------------------------------------

Now in the reciever.cs file just call the method AddHandler inside the FeatureActivated Mehod.

This will enable the event handler to be attached to the TestList as soon as the feature is activated on thesite


  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb objweb = (SPWeb)properties.Feature.Parent;
                SPList objTestList= objweb.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title,TestList, true) == 0);
                //Attach the ItemAdded event and the ItemDeletingEvent
                if (objTestList!= null)
                {
                   
                    AddHandler(objTestList, "AddingEventHandler", 10003, SPEventReceiverType.ItemAdding);
                    AddHandler(objTestList, "DeletingEventHandler", 10002, SPEventReceiverType.ItemDeleting);
                                    
                    
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, CommonConstants.ITI_EXCEPTION);
            }

        }

--------------------------------------------------------------------------------------------------


Happy SharePointing again :)


No comments:

Post a Comment

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...