In this second post on Remote Event Receivers we are going to explore the implementation details of “Remote Event Handlers for App Events”.
In case you need to recall the concepts related to “Remote Event Receivers”, you can refer to my First post in this series SharePoint 2013: Remote Event Receivers
In order to show case the implementation details of Remote Event Handler for App Events, let’s start with creating a Provider Hosted App by using following steps:
Create New Project in Visual Studio using “App for SharePoint” Project template
Specify Host Web URL and choose “Provider-hosted” as App Type
Choose “ASP.Net Web Forms Application” as Project Template to create Remote Web for our Provider Hosted App
Specify Certification Details based on the configuration of Provider-Hosted App Development Environment. Following details needs to be provided:
- Certificate Location
- Issuer ID
Once all the above steps executed successfully we will get a new Solution created with two projects:
- PH-AppEventLifeCycle which is a Provider Hosted App
- PH-AppEventLifeCycleWeb which is a Remote Web for App
Select Provider Hosted App Project and press F4 to see the Project Properties. In the Project Properties look for “App for SharePoint Events” section and enable all events that you want to get handled by Remote Event Receiver.
Here I have set all the three events “Handle App Installed”, “Handle App Uninstalling, “Handle App Upgraded” to True, this setting will allow SharePoint to delegate respective events to the registered Event Receiver.
In case you need to create an App Web for your Provider Hosted Web (though it is not necessary) you must have atleast one Web Scoped Artifact added in the Project as this action will force SharePoint App Framework to provision App Web during the App Deployment.
In this case I have added a Dummy Module deploying some sample file just to force SharePoint to create an App Web for our Provider Hosted App.
It is noteworthy to look for AppManifest.xml file as most of the configuration settings for an App are derived from here only.
In General Tab, we have two noteworthy Properties:
- Start Page: It allows you to set any Page as App Start Page. In this case we have set it point to the Default.aspx Page in Remote Web.
- Query String : Allows adding additional information as Query String Parameters while redirecting to the Start Page
In Permission Tab, we can specify the set of Permissions that App will need to perform desired tasks.
At the time of App Installation, App will request this permission set to be granted and that we will see few steps down the line.
Now the very next thing is to investigate the constitution of Remote Web Project “PH-AppEventLifeCycleWeb”
In this Project we have following important Files to look for:
- Default.aspx: This is the start page for Provided Hosted App as we set it in earlier steps. In this page we can perform actions that are desired for a specific task. For example we can provide UI for end users to interact with the App.
In this demo the code sample is reading the title of the Hosting Web as follows:
Steps 1: Getting URL of the Host Web by reading “SPHostUrl” Parameter
Steps 2: Instantiating Client Context by Calling GetS2SClientContextWithWindowsIdentity method provided by SharePoint Infrastructure by means of TokenHelper.cs Class
Step 3: Once Client Context is Instantiated, we can make use of Managed CSOM to load the Web and read its Title Property as shown below
- Scripts: We can go with the default set of scripts added during creation of the project, else we can add any desired script file to it
- AppEventReceiver.svc : This Service class has been added to the Project as soon as you add a “Remote Event Receiver” Project Item to the Project
Let’s walkthrough through the code file and see what we got.
Step 1: Add “Microsoft.SharePoint.Client.EventReceivers” Namespace which is needed for Remote Service to handle Remote Events and inherit the class from “IRemoteEventService”
Step 2: Override the method ProcessEvent
Step 3: Specify Remote Event Service Status if you want to continue or reject, so that execution succeed or revert back
Step 4: Perform actions as per the business requirement. Here I am adding logs to Windows Event Log
Step 5: Return the Event Result back to SharePoint
Step 1: Override the method ProcessOneWayEvent
Step 2: Perform actions as per the business requirement. Here I am adding logs to Windows Event Log
- Web.config: In Web.config file, there are a couple of “AppSettings” that are important to take note of-
ClientId: Generated Automatically by Visual Studio for development perspective. At the time of App registration this can be regenerated and used accordingly.
ClientSigningCertificatePath: Specify the path of Client Certificate exported during environment configuration.
ClientSigningCertificatePassword: Specify the path of Client Certificate Password specified to protect the Certificate during environment configuration.
IssuerId: Specify the path of Issuer ID generated during environment configuration
With this we are done with inspection to all of the important files in our solution.
Now it is time to Build the Solution and Run it.
In the below screen shot we can see the Client ID is generated by Visual Studio Tools during Build Process.
Once the Solution Build & Run successfully, the App Framework looks for the AppManifest.xml and find a permission set to be granted by App User on the Host Web.
So we have to grant permissions as specified in Permissions Tab of AppManifest.xml file and in order to grant the permissions click “Trust It”.
As soon as we grant the permissions, App Launcher looks for the App Start Page as specified in General Tab of AppManifest.xml file and Redirect the user to that page.
If we notice the URL in below screen shot, we find the App Start Page is default.aspx of Remote Web as specified in AppManifest.xml file for the App.
If we investigate the Site Structure using any tool like SharePoint Manager (one of my favorites), we can see an App Web by the name “PH-AppEventLifeCycle” is also provisioned due to the presence of Dummy Module we added to the Project earlier.
After App is getting installed successfully, we can also see an event log is added to the Windows Application Log as shown below based on the message we placed in the “AppEventReceiver.svc” code file:
Likewise while the App is uninstalling, we can see an event log is added to the Windows Application Log as shown below based on the message we placed in the “AppEventReceiver.svc” code file:
This simple walkthrough can help you to understand on how to deal with the App Events.
We can utilize these semantics under different business cases liking registering Event Receivers for existing lists or notifying users and so on.
Hope you find it helpful.
See you all in the next post of this series. 🙂