AppInstalled Event Receivers in SharePoint Office 365 – An Insight


Sathish Nadarajan
SharePoint MVP
Published On :   07 Aug 2017
Visit Count
Today :  1    Total :   793
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint