How to Attach Event Receivers to a Web in SharePoint Office 365 Programmatically using CSOM C#

Sathish Nadarajan
 
Solution Architect
January 24, 2017
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
3612

In this article, we will be seeing how to add the Event Receiver to a Web in SharePoint Office 365 Programmatically using CSOM C#

 
 
 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Microsoft.SharePoint.Client.Taxonomy;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
     using System.Threading.Tasks;
 
     class Program
     {
         static void Main(string[] args)
         {
             AttachEventReceivers();
         }
 
 
         public static void AttachEventReceivers()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*********.sharepoint.com/sites/CommunitySite/";
             string userName = "Sathish@*****.onmicrosoft.com";
             string password = "*********";
 
 
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.Load(web.EventReceivers);
                 ctx.ExecuteQueryRetry();
 
                 #region Attach Document List Created Event Receiver
 
                 var receiver = new EventReceiverDefinitionCreationInformation();
 
                 receiver.EventType = EventReceiverType.ListAdded;
                 receiver.ReceiverUrl = new Uri("https://<<RemoteEventReceiverSite.com/Services/Process.svc>>").AbsoluteUri;
                 receiver.ReceiverName = "<<Receiver Name>>";
                 receiver.Synchronization = EventReceiverSynchronization.Asynchronous;
 
                 web.EventReceivers.Add(receiver);
 
                 ctx.ExecuteQueryRetry();
 
                 #endregion
 
                 System.Console.ReadLine();
             }
         }
 
 
     }
 
 }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment