无法更新Sharepoint网站的权限

时间:2018-09-07 11:59:39

标签: c# office365 csom

我想出了CSOM代码来更新权限,但是由于某种原因,更新没有发生。我刷新网站,权限设置保持不变。代码有什么问题吗?

        private static void UpdatePermissions(ClientContext clientContext)
    {

        try
        {
            clientContext.Load(clientContext.Web);
            clientContext.Load(clientContext.Web.RoleDefinitions);
            clientContext.ExecuteQuery();

            var roleDefinitions = clientContext.Web.RoleDefinitions;

            var ownerPermissions = roleDefinitions.GetByName("Owner");

            clientContext.Load(ownerPermissions);
            clientContext.ExecuteQuery();

            ownerPermissions.BasePermissions.Set(PermissionKind.DeleteVersions);
            ownerPermissions.BasePermissions.Set(PermissionKind.UseClientIntegration);
            ownerPermissions.BasePermissions.Set(PermissionKind.UseRemoteAPIs);

            //clientContext.Load(ownerPermissions);
            clientContext.Web.Update();
            clientContext.ExecuteQuery();
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("UpdatePermissions " + e.Message);
            WriteLog(fileName, "UpdatePermissions " + e.Message);
        }
        finally
        {
            Console.ForegroundColor = ConsoleColor.White;
        }
    }

1 个答案:

答案 0 :(得分:1)

找到了解决方案

        private static void UpdatePermissions(ClientContext clientContext)
    {

        try
        {
            clientContext.Load(clientContext.Web);
            clientContext.Load(clientContext.Web.RoleDefinitions);
            clientContext.ExecuteQuery();

            var roleDefinitions = clientContext.Web.RoleDefinitions;

            var ownerPermissions = roleDefinitions.GetByName("Owner");

            clientContext.Load(ownerPermissions);
            clientContext.ExecuteQuery();

            var basePermissions = CopyBasePermissionLevel(ownerPermissions.BasePermissions);

            basePermissions.Set(PermissionKind.DeleteVersions);
            basePermissions.Set(PermissionKind.UseClientIntegration);
            basePermissions.Set(PermissionKind.UseRemoteAPIs);

            ownerPermissions.BasePermissions = basePermissions;
            //clientContext.Load(ownerPermissions);
            ownerPermissions.Update();
            clientContext.ExecuteQuery();
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("UpdatePermissions " + e.Message);
            WriteLog(fileName, "UpdatePermissions " + e.Message);
        }
        finally
        {
            Console.ForegroundColor = ConsoleColor.White;
        }
    }

    private static BasePermissions CopyBasePermissionLevel(BasePermissions basePermission)
    {
        var copiedPermissions = new BasePermissions();
        foreach (var permission in Enum.GetValues(typeof(PermissionKind)))
        {
            if (basePermission.Has((PermissionKind) permission))
            {
                copiedPermissions.Set((PermissionKind)permission);
            }
        }
        return copiedPermissions;
    }