Attach Event Receiver to SharePoint List using CSOM (Client Side Object Model)

  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.Net;  
  4.   
  5. private static void AttachEventReceiver()  
  6.         {  
  7.             var userName = "<<UserNameWithoutDomainName>>";  
  8.             var password = "<<Password>>";  
  9.             string domain = "<<DomainName>>";  
  10.             string webUrl = "<<WebUrl>>";  
  11.   
  12.             using (var context = new ClientContext(webUrl))  
  13.             {  
  14.                 context.Credentials = new NetworkCredential(userName, password, domain);  
  15.                 var list = context.Web.Lists.GetByTitle("<<ListName>>");  
  16.                 var eventReceivers = list.EventReceivers;  
  17.                 context.Load(eventReceivers);  
  18.                 context.ExecuteQuery();  
  19.   
  20.                 EventReceiverType eventType = EventReceiverType.ItemUpdated;    //event type (ItemAdded / ItemUpdated)  
  21.                 string receiverAssembly = "<<FullyQualifiedAssemblyName>>";     //Example: <<AssemblyName>>, Version=<<AssemblyVersion>>, Culture=neutral, PublicKeyToken=<<PublicKeyToken>>  
  22.                 string receiverClass = "<<ReceiverClassNameStartingWithNamespaceName>>";    //Example: <<NamespaceName>>.<<ClassName>>  
  23.                 string receiverName = "<<ReceiverName>>";       //you can give any name  
  24.   
  25.                 bool isEventReceiverAlreadyAssociated = false;  
  26.                 foreach (var eventReceiver in eventReceivers)  
  27.                 {  
  28.                     if (eventReceiver.EventType == eventType  
  29.                             && string.Compare(eventReceiver.ReceiverAssembly, receiverAssembly, StringComparison.OrdinalIgnoreCase) == 0  
  30.                             && string.Compare(eventReceiver.ReceiverClass, receiverClass, StringComparison.OrdinalIgnoreCase) == 0  
  31.                             && string.Compare(eventReceiver.ReceiverName, receiverName, StringComparison.OrdinalIgnoreCase) == 0  
  32.                         )  
  33.                     {  
  34.                         isEventReceiverAlreadyAssociated = true;  
  35.                     }  
  36.                 }  
  37.   
  38.                 if (!isEventReceiverAlreadyAssociated)  
  39.                 {  
  40.                     EventReceiverDefinitionCreationInformation eventReceiver  
  41.                                        = new EventReceiverDefinitionCreationInformation  
  42.                                          {  
  43.                                                EventType = eventType,  
  44.                                                ReceiverAssembly = receiverAssembly,  
  45.                                                ReceiverClass = receiverClass,  
  46.                                                ReceiverName = receiverName,  
  47.                                           };  
  48.   
  49.                     list.EventReceivers.Add(eventReceiver);  
  50.                     context.ExecuteQuery();  
  51.                 }  
  52.             }  
  53.         }