Step By Step Procedures to Add a Remote Event Receivers (RER) in SharePoint Office 365 – A simplest Way

Sathish Nadarajan
 
Solution Architect
January 30, 2017
 
Rate this article
 
Views
8783

I have been seeing many developers, who is really scary about creating the Remote Event Receivers. But actually the RER are very useful utilities and it can be customized for many things. So thought of making it as simple as It could be.

Let us see the Process Step by Step.

1. Before start reading, I assume that all of us gone through the Earlier Article HERE.

2. If not, I strongly suggest that, as this will be a continuation from that.

3. Now, one more important URL we need to go through is, how to attach an Event Receiver.

4. Basically, I am going to update the app web alone.

5. And by using a small piece of EXE, we will attach the remote event receiver with the List.

6. Now, for our reference, the initial Structure of the APP which we created in the earlier article will be like this.

 

image

7. The source code can be downloaded at the provided link at the end.

8. Add a folder called Services.

 

 

 

9. Add a Txt file and Rename it as “Validation.svc”

10. Right Click and “View Markup”

 

image

 

11. Edit the Markup as below.

 <%@ ServiceHost Language="C#" Debug="true" Service="Office365.PHA.OnPrem.APPWeb.Services.Validation" CodeBehind="Validation.svc.cs" %>

12. Rebuild the solution.

13. That’s it. Now, let us add the remote event receiver to a List in the Site.

14. For that, we are already seen that in a different article. For our reference, the code would be,

 using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.Load(web.Lists);
                 ctx.ExecuteQueryRetry();
 
                 List myList = web.Lists.GetByTitle("MyList");
                 ctx.Load(myList);
                 ctx.Load(myList.EventReceivers);
                 ctx.ExecuteQueryRetry();
 
                 #region Attach Document Item Created Event Receiver
 
                 var receiver = new EventReceiverDefinitionCreationInformation();
 
                 receiver.EventType = EventReceiverType.ItemAdded;
                 receiver.ReceiverUrl = new Uri("http://office365pha/Office365.PHA.OnPrem.APPWeb/Services/Validation.svc").AbsoluteUri;
                 receiver.ReceiverName = "MyItemAddingEvent2";
                 receiver.Synchronization = EventReceiverSynchronization.Asynchronous;
 
                 myList.EventReceivers.Add(receiver);
 
                 ctx.ExecuteQueryRetry();
 
                 #endregion
 
                  
             }
 

15. The Web.Config would be like below.

 <?xml version="1.0" encoding="utf-8"?>
 <!--
   For more information on how to configure your ASP.NET application, please visit
   http://go.microsoft.com/fwlink/?LinkId=169433
   -->
 <configuration>
   <system.web>
 
     <authentication mode="None" />
     <customErrors mode="Off"></customErrors>
     <httpRuntime targetFramework="4.5"/>
   </system.web>
 
 
 
 
 
    
 
   <location path="Services">
     <system.web>
       <authorization>
         <allow users="*" />
       </authorization>
     </system.web>
   </location>
   
   <system.codedom>
     <compilers>
       <compiler language="c#;cs;csharp" extension=".cs"
         type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
       <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
         type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE="Web" /optionInfer+"/>
     </compilers>
   </system.codedom>
   
   
   <appSettings>
     <add key="ClientId" value="***********" />
     <add key="ClientSecret" value="***************" />
 
      
     
   </appSettings>
   
   
   <system.serviceModel>
     <bindings>
       <basicHttpBinding>
         <binding name="secureBinding">
           <security mode="Transport" />
         </binding>
         <!--Used by SharePoint Add-in-->
         <binding name="secureBinding1">
           <security mode="Transport" />
         </binding>
         <!--Used by SharePoint Add-in-->
         <binding name="secureBinding2">
           <security mode="Transport" />
         </binding>
       </basicHttpBinding>
     </bindings>
     <protocolMapping>
       <add binding="basicHttpBinding" scheme="https" bindingConfiguration="secureBinding2" />
     </protocolMapping>
   </system.serviceModel>
 </configuration>
 

 

DOWLOAD HERE

 

 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment