处理已安装的应用程序事件.替换上下文权限不起作用 [英] Handle app event Installed . Replace permissions for context isn't working

查看:81
本文介绍了处理已安装的应用程序事件.替换上下文权限不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

当我安装或升级mu app时,我想创建组,或者如果存在则替换权限.但是,每当我使用应用程序事件接收器时,我什至无法在共享点上安装我的应用程序.如果我不使用它们,则该应用程序会很好地安装,并列出项目事件接收器 也可以.

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.

当我启用了应用程序事件接收器时,我也无法调试.我不知道为什么,但是Visual Studio在提供凭据后卡住了

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();

    }
}
}



推荐答案

感谢您的信息.

这是一条简短的注释,旨在让您知道我们正在对此问题进行研究.

This is a quick note to let you know that we are performing research on this issue.

最好的问候,

Lee


这篇关于处理已安装的应用程序事件.替换上下文权限不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆