About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday 11 December 2013

Multiple SharePoint Event handlers using Visual Studio 2010

Introduction
You may come accross a situation where you have already added one event handler for a single List event and now you want to add another event on the Custom List or some other type of list or document library. In this scenario what will you do? Will you add another Event Receiver item from New Item dialog box?

Here I am describing the way to add more event handlers in the same Event Receiver file.

Solution

Create a event handler on a custom list. See Create Event Handlers for Sharepoint List.

Open the Element.xml. It will look like the code below:
//

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

     <Receivers ListTemplateId="100">

          <Receiver>

               <Name>CustomEventReceiversItemAdded</Name>

               <Type>ItemAdded</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

             </Receiver>

           </Receivers>

</Elements>

//
In code behind file, corrosponding method will look like the code below:
public override void ItemAdded(SPItemEventProperties properties)

{

   base.ItemAdded(properties);

}
Now for adding one more event receiver for custom list event ItemAdding, copy the Receiver tag and paste just below the first Receiver tag (see below code).
Change the Name and Type node values. Node value should be same as event name
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

   <Receivers ListTemplateId="100">

         <Receiver>

               <Name>CustomEventReceiversItemAdded</Name>

               <Type>ItemAdded</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

          <Receiver>

               <Name>CustomEventReceiversItemAdding</Name>

               <Type>ItemAdding</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

     </Receivers>

</Elements>
Go to the code behind file and add one event for ItemAdding
public override void ItemAdding(SPItemEventProperties properties)

{

   base.ItemAdding(properties);

}

Now check the condition of your listname and paste your code inside the condition
public override void ItemAdding(SPItemEventProperties properties)

{

   base.ItemAdding(properties);

   if (properties.List.Title == "ListName")

   {

   }

}
This code will handle the events on CustomList as we have used ListTemplateId = 100 in Element.xml. 100 is the ListTemplateId for CustomList.
What will you do if a requirement comes to handle Document Library events?
To handle Document Library events, there is not need to add one more EventReceiver module. You only need to add one more Receivers tag with ListTemplateId = 101. 101 is for Document Library.
I am adding both ItemAdded and ItemAdding events in this example. Element.xml will looks similer to below code:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

     <Receivers ListTemplateId="100">

          <Receiver>

               <Name>CustomEventReceiversItemAdded</Name>

               <Type>ItemAdded</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

          <Receiver>

               <Name>CustomEventReceiversItemAdding</Name>

               <Type>ItemAdding</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

     </Receivers>

     <Receivers ListTemplateId="101">

          <Receiver>

               <Name>CustomDocEventReceiversItemAdded</Name>

               <Type>ItemAdded</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

          <Receiver>

               <Name>CustomDocEventReceiversItemAdding</Name>

               <Type>ItemAdding</Type>

               <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

               <Class>MyApp.CustomEventReceivers.CustomEventReceivers</Class>

               <SequenceNumber>10000</SequenceNumber>

          </Receiver>

     </Receivers>

</Elements>

Change the value of Name node to avoid conflict. You don't need to change the Class and Assembly nodes value as we are using the same code behind file and same class to handle these events.
Use the same methods in code behind to handle these events for Document Library. Put the ListName condition and paste your code inside that.

Similerly you can add more event handlers for different type of lists or libraries in a single module.

No comments:

Post a Comment