none
Handle app event Installed . Replace permissions for context isn't working RRS feed

  • Question

  • Hi guys!

    When i install or upgrade mu app i want to create groups or if exist to replace permissions. However whenever i use the app event receivers, i can't even install my app on sharepoint. If i don't use them the app installs just fine , and list item event receivers work as well.

    Also i can't debug when i have enabled the app event receivers. I don't know why but visual studio is stuck after providing credentials

    Here is the code i'm using :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.EventReceivers;
    
    namespace LeaveRequestAppWeb.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();
    
            using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
            {
                if (clientContext != null)
                {
                    clientContext.Load(clientContext.Web);
                    clientContext.ExecuteQuery();
                    try
                    {
                        if (properties.EventType == SPRemoteEventType.AppInstalled || properties.EventType == SPRemoteEventType.AppUpgraded)
                        {
    
                            var ifGroupExist = false;
                            var ifManagerGroupExist = false;
                            var ifAdminGroupExist = false;
    
                            var siteGroups = clientContext.Web.SiteGroups;
                            for (var i = 0; i < siteGroups.Count; i++)
                            {
                                if ("Leave Request Members" == siteGroups[i].Title)
                                {
                                    ifGroupExist = true;
    
                                }
    
                                if ("Leave Request Managers" == siteGroups[i].Title)
                                {
                                    ifManagerGroupExist = true;
    
                                }
    
                                if ("Leave Request Administrators" == siteGroups[i].Title)
                                {
                                    ifAdminGroupExist = true;
    
                                }
                            }
    
                            // create SP group if it does not exist
                            if (!ifGroupExist) { createSPGroup(clientContext, "Leave Request Members"); }
    
                            if (!ifManagerGroupExist) { createSPGroup(clientContext, "Leave Request Managers"); }
    
                            if (!ifAdminGroupExist) { createSPGroup(clientContext, "Leave Request Administrators"); }
    
    
                            if (ifGroupExist) { replacePermissions(clientContext, "Leave Request Members"); }
    
                            if (ifManagerGroupExist) { replacePermissions(clientContext, "Leave Request Managers"); }
    
                            if (ifAdminGroupExist) { replacePermissions(clientContext, "Leave Request Administrators"); }
    
    
                        }
                    }
                    catch (Exception e)
                    {
                        result.ErrorMessage = e.Message;
                        result.Status = SPRemoteEventServiceStatus.CancelWithError;
                    }
                }
            }
    
            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();
        }
    
        public void createSPGroup(ClientContext clientContext, String groupName)
        {
            var web = clientContext.Web;
            var groupCollection = web.SiteGroups;
            // Create Group information for Group  
            var newGRP = new GroupCreationInformation();
            newGRP.Title = "groupName";
            newGRP.Description = "sample description";
    
    
    
            clientContext.Load(web);
            clientContext.ExecuteQuery();
    
            if (!web.HasUniqueRoleAssignments)
            {
                web.BreakRoleInheritance(false, true);
            }
    
            var rolDef = web.RoleDefinitions.GetByName("Contribute");
            //add group to site gorup collection  
            var newCreateGroup = groupCollection.Add(newGRP);
            //Role Definition
            if (groupName == "Leave Request Members")
            {
                rolDef = web.RoleDefinitions.GetByName("Contribute");
            }
            else if (groupName == "Leave Request Managers")
            {
                rolDef = web.RoleDefinitions.GetByName("Edit");
            }
    
            else if (groupName == "Leave Request Administrators")
            {
                rolDef = web.RoleDefinitions.GetByName("Full Control");
            }
    
            var rolDefColl = new RoleDefinitionBindingCollection(clientContext);
            rolDefColl.Add(rolDef);
    
            // Get the RoleAssignmentCollection for the target web.  
            var roleAssignments = web.RoleAssignments;
            // assign the group to the new RoleDefinitionBindingCollection.  
            roleAssignments.Add(newCreateGroup, rolDefColl);
            //Set group properties  
            newCreateGroup.AllowMembersEditMembership = true;
            newCreateGroup.Update();
            clientContext.Load(newCreateGroup);
            clientContext.ExecuteQuery();
        }
    
        public void replacePermissions(ClientContext clientContext, String groupName)
        {
            var web = clientContext.Web;
    
            var groupCollection = web.SiteGroups;
    
            var group = groupCollection.GetByName(groupName);
    
            clientContext.Load(web);
            clientContext.ExecuteQuery();
    
            if (!web.HasUniqueRoleAssignments)
            {
                web.BreakRoleInheritance(false, true);
    
            }
    
            var rolDef = web.RoleDefinitions.GetByName("Contribute");
    
            if (groupName == "Leave Request Members")
            {
                rolDef = web.RoleDefinitions.GetByName("Contribute");
            }
            else if (groupName == "Leave Request Managers")
            {
                rolDef = web.RoleDefinitions.GetByName("Edit");
            }
    
            else if (groupName == "Leave Request Administrators")
            {
                rolDef = web.RoleDefinitions.GetByName("Full Control");
            }
    
            var rolDefColl = new RoleDefinitionBindingCollection(clientContext);
            rolDefColl.Add(rolDef);
    
            // Get the RoleAssignmentCollection for the target web.  
            var roleAssignments = web.RoleAssignments;
            // assign the group to the new RoleDefinitionBindingCollection.  
            roleAssignments.Add(group, rolDefColl);
            //Set group properties
            clientContext.Load(group);
            clientContext.ExecuteQuery();
    
        }
    }
    }

    Friday, January 19, 2018 10:58 AM

All replies