4

I have created an Event Receiver Assembly and installed in GAC as shown here. In that assembly I have override ItemAdded and ItemUpdated events.

The assembly has been successfully installed in GAC.

Now what I want is to attach that event receiver to my document library in SP2010 using CSOM C#.

I found the great article Attach Event Receiver to SharePoint List using CSOM (Client Side Object Model).

Code :

 using (ClientContext clientContext = new ClientContext(siteUrl))
{

    List documentLibrary = clientContext.Web.Lists.GetByTitle("TestList");
    var eventReceivers = documentLibrary.EventReceivers;
    clientContext.Load(eventReceivers);
    clientContext.ExecuteQuery();

    EventReceiverType eventType = EventReceiverType.ItemAdded;
    string receiverAssembly = "EventHandlerName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8996123dfc8d0aa2";
    string receiverClass = "EventHandlerName.EventHandler";
    string receiverName = "Item Added Event";       //you can give any name  

    bool isEventReceiverAlreadyAssociated = false;
    foreach (var eventReceiver in eventReceivers)
    {
        if (eventReceiver.EventType == eventType
                && string.Compare(eventReceiver.ReceiverAssembly, receiverAssembly, StringComparison.OrdinalIgnoreCase) == 0
                && string.Compare(eventReceiver.ReceiverClass, receiverClass, StringComparison.OrdinalIgnoreCase) == 0
                && string.Compare(eventReceiver.ReceiverName, receiverName, StringComparison.OrdinalIgnoreCase) == 0
            )
        {
            isEventReceiverAlreadyAssociated = true;
        }
    }

    if (!isEventReceiverAlreadyAssociated)
    {
        EventReceiverDefinitionCreationInformation eventReceiver
            = new EventReceiverDefinitionCreationInformation
            {
                EventType = eventType,
                ReceiverAssembly = receiverAssembly,
                ReceiverClass = receiverClass,
                ReceiverName = receiverName,
            };

        documentLibrary.EventReceivers.Add(eventReceiver);
        clientContext.ExecuteQuery();
    }
}

But it seems that this is for SP2013.

  • When I used the SharePoint.Client.Runtime and SharePoint.Client dll of version 14.0.0.0, then it shows that EventReceiverType does not exists
  • When I used the SharePoint.Client.Runtime and SharePoint.Client dll of version 15.0.0.0, then it gave me error

Field or property "EventReceivers" does not exist

My question is : How we can attach the event receiver to the List/Library using CSOM C# for SP2010?

I have already gone through many posts on SE but some does not have solutions and some are for SP2013

  • Whats the basic reason behind adding event receiver via CSOM? – Taran Goel Sep 5 '16 at 7:17
  • I am not having SharePoint Environment. I have just client machine so I am using C# CSOM (Not Powershell too) – Aakash Maurya Sep 5 '16 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.