0

I have an item updated event receiver that I would like to associate with only 2 document libraries.

Earlier I had associated the event receiver with ListTemplateId="101". So this event receiver was firing for all document libraries! which is something I didn't want but I was not able to avoid it cleanly. So as a workaround in the first line of code I check the library in which the event receiver is called and and returned if it was not the intended library.

Then I read a bit about receivers tags in elements.xml and found that I can register multiple receivers tags in elements.xml. So i changed the elements.xml like below

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="DocLib1">
    <Receiver>
      <Name>EventReceiver1ItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>NM1.EventReceiver1.EventReceiver1</Class>
      <SequenceNumber>10000</SequenceNumber>
      <Synchronization>Synchronous</Synchronization>
    </Receiver>
  </Receivers>
  <Receivers ListUrl="DocLib2">
    <Receiver>
      <Name>EventReceiver1ItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>NM1.EventReceiver1.EventReceiver1</Class>
      <SequenceNumber>10000</SequenceNumber>
      <Synchronization>Synchronous</Synchronization>
    </Receiver>
  </Receivers>
</Elements>

Hoping that the event receiver will be associated with only these 2 doc libs. But the fact is that the event receiver is attaching only to DocLib1 (the first receivers in elements.xml)

Can someone guide me if what I am trying to achieve will be possible with the approach that I am following or I should change my approach?

Thanks

Amar

Amar
  • 27
  • 1
  • 9

2 Answers2

0

There's a good answer to this problem here.

But a different quick and dirty solution would be to revert your elements.xml to using ListTemplateId="101" in a single Receiver element, and wrapping your logic in the following:

if(properties.ListTitle == "List1" || properties.ListTitle == "List2")
{
    // Your logic here.
}
DevBot
  • 427
  • 1
  • 7
  • 31
  • This is an existing application, the doc libs already contain a large amount of data. I am not too comfortable playing around with them in production. About the second solution that you suggested, I have already implemented it (**So as a workaround in the first line of code I check the library in which the event receiver is called and and returned if it was not the intended library.**). – Amar Sep 22 '17 at 08:38
0

I had been trying hard to achieve it using elements.xml but could not in due time so I resorted to using feature activation event code.

I modified elements.xml file so as to not associate this event to anything

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers>
    <Receiver>
      <Name>EventReceiver1ItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>NM1.EventReceiver1.EventReceiver1</Class>
      <SequenceNumber>10000</SequenceNumber>
      <Synchronization>Synchronous</Synchronization>
    </Receiver>
  </Receivers>      
</Elements>

And then by attaching event to my feature i modified the FeatureActivated event and wrote something like this

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            using (SPWeb web = site.RootWeb)
            {
                web.Lists["DoocLib1"].EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "NM1.EventReceiver1.EventReceiver1");
                web.Lists["DoocLib2"].EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "NM1.EventReceiver1.EventReceiver1");

            }
        }

Please note that the feature should be scoped to at least Site for this to work.

I will keep exploring ways to achieve it using elements.xml and post it when I have a solution.

Thanks

Amar

Amar
  • 27
  • 1
  • 9