SharePoint 2013: App Event Receivers

Recently I got rolled off from a SharePoint 2010 project for few weeks. So, I decided to spend some of my free time on SharePoint 2013. Since apps are a new concept in SP 2013 I decided to create some sample apps. I did not make any production apps as I am yet to get allocated on a project which uses SP 2013 apps. In this series of blog posts I will try to share my leanings. 
This series will consist of following posts:
Develop SharePoint 2013 apps with NAPA
Access data in Host web
Isolated App domain Setup Issues
Create SharePoint Hosted App in Visual Studio
Provider Hosted App Development Prerequisites
App Event Receivers
Remote Event Receivers
In this post we will see how to add an app installed event to the Provider Hosted app. We will also look how to debug the app installed event. During this event a SharePoint Group will be created in the main site collection.

Create a Provider Hosted SharePoint app in Visual Studio.

Click on the web project and Press F4 to bring up project properties. Change SSL Enabled to False in case SSL is disabled.
Now click on app project and press F4 to open app project properties. Set Handle App Installed Event to true. 
Notice a WCF service is automatically created with some sample code.
The sample code inside the service file has two methods ProcessEvent
which handles the app events and ProcessOneWayEvent which is not used by app events as mentioned in the comments. Add following code to the file.
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
    SPRemoteEventResult result = new SPRemoteEventResult();
    if (properties.EventType == SPRemoteEventType.AppInstalled)
    {
        using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
        {
            if (clientContext != null)
            {
                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();
                Web oWebsite = clientContext.Web;
                GroupCollection collGroup = clientContext.Web.SiteGroups;
                clientContext.Load(collGroup);
                clientContext.ExecuteQuery();

                // Create few predefined groups
                CreateGroup(collGroup, "SampleAppGroup", oWebsite, clientContext);
            }
        }
    }
    return result;
}

private void CreateGroup(GroupCollection collGroup, string groupName, Web oWebsite, ClientContext clientContext)
{
    Group grp = collGroup.Where(g => g.Title == groupName).FirstOrDefault();
    if (grp == null)
    {
        GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
        groupCreationInfo.Title = groupName;
        groupCreationInfo.Description = "Description of " + groupName;
        Group oGroup = oWebsite.SiteGroups.Add(groupCreationInfo);
        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
        RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByType(RoleType.Contributor);
        collRoleDefinitionBinding.Add(oRoleDefinition);
        oWebsite.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
        clientContext.Load(oGroup, group => group.Title);
        clientContext.Load(oRoleDefinition, role => role.Name);
        clientContext.ExecuteQuery();
    }
}
The code above checks if the EventType is AppInstalled event. It then creates a ClientContext object of the host web by passing false to the method TokenHelper.CreateAppEventClientContext(properties, false). Passing true instead of false will create ClientContext object of app web. The code loads all SharePoint Groups present in the site collection and then creates a group named "SampleAppGroup" if it is not present and attaches Contribute permissions to  the group. We need to give "Full Control" permissions to the app in order to run the above code. Open AppManifest.xml file in the app project and assign the permissions.
Right Click on the web project and select properties. Select Web tab and make sure that "Use IIS Express" is checked. 
Put a break point in ProcessEvent method and press F5 to deploy the app. Observe that the break point is hit after the application is granted access by trusting it.
When the code is fully executed the Default.aspx page of the web project will show up. It contains the code to show the title of the Host Web site.
Stop debugging and open the site where the app was installed and go to People and Groups section of the site. Notice that a new group has been created.

profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+