AppInstalled Event Receivers in SharePoint Office 365 – An Insight

Sathish Nadarajan
 
Solution Architect
August 7, 2017
 
Rate this article
 
Views
3432

We have seen, many remote event receivers through the Provider Hosted APP. But, those event receivers will be ItemAdded, Updated etc., But, there is three more events, which is APPInstalled, Upgrated, UnIinstalling Events, which plays a vital role. There are many scenarios, which can be handled by the APP Installed Events.

Say for example, in my case, I wanted to create a List in the Host Web as part of the APP Installation. i.e., Whenever the APP is installed on any of the site collection, I need to set up a set of activities like, creating site columns, content types, lists and attach some other event receivers to the newly created list. Usually, these things should be handled by a Provisioning EXE. If the APP is given as a standalone package, then there may not be a chance to run a Provisioning EXE. So, we need to rely on the AppManifest.XML file only. Let us see, how to do that.

1. Through visual Studio.

Ø On the APP Solution, right click on the APP project and select the properties.

clip_image002

Ø The three events at the bottom does the required things for us.

Ø By default, they will be False.

Ø Make it as TRUE.

Ø As soon as we make it as true, there will be a SVC file generated on the APP Web Project. And the manifest file will be updated as below.

 <Properties>
     <Title>JBBoda.EventReceiver</Title>
     <StartPage>https://***.*************.com/appwebproject/Pages/Default.aspx?{StandardTokens}</StartPage>
     <InstalledEventEndpoint> https://***.*************.com/appwebproject /Services/AppEventReceiver.svc</InstalledEventEndpoint>
   </Properties>
 

2. Through the attach Event Receiver console application. We have already seen this in one of the earlier article.

On the SVC file, the default code snippet will be like the below.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.EventReceivers;
 
 namespace JBBoda.EventReceiverWeb.Services
 {
     public class AppEventReceiver : IRemoteEventService
     {
         /// <summary>
         /// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled.
         /// </summary>
         /// <param name="properties">Holds information about the app event.</param>
         /// <returns>Holds information returned from the app event.</returns>
         public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
         {
             SPRemoteEventResult result = new SPRemoteEventResult();
             if (properties.EventType == SPRemoteEventType.AppInstalled)
             {
                 using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, useAppWeb: false))
                 {
                     if (clientContext != null)
                     {
                         clientContext.Load(clientContext.Web);
                         clientContext.ExecuteQuery();
 
 // This is the place, where I am implementing all my Logics.  i.e., Creating the List, update the list etc.,
                         clientContext.Web.CreateList(ListTemplateType.GenericList, "Test List", false, true, "", true);
                         clientContext.ExecuteQuery();
                     }
                 }
             }
             return result;
         }
 
         /// <summary>
         /// This method is a required placeholder, but is not used by app events.
         /// </summary>
         /// <param name="properties">Unused.</param>
         public void ProcessOneWayEvent(SPRemoteEventProperties properties)
         {
             throw new NotImplementedException();
         }
 
     }
 }
 

The above app installed event receiver will create a list called “Test List” during the app Installation.

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