Example #1
0
        private void SetupPermissionSchemeOfList(ClientContext context, List list, ShList listConfig)
        {
            if (listConfig.PermissionScheme != null)
            {
                if (listConfig.PermissionScheme.BreakInheritance)
                {
                    list.BreakRoleInheritance(true, false);
                    list.Update();
                    context.ExecuteQuery();
                }
                if (listConfig.PermissionScheme.RemoveDefaultRoleAssignments)
                {
                    context.Load(list.RoleAssignments);
                    context.ExecuteQuery();
                    for (var i = list.RoleAssignments.Count - 1; i >= 0; i--)
                    {
                        list.RoleAssignments[i].DeleteObject();
                    }
                }
                foreach (var roleAssignment in listConfig.PermissionScheme.RoleAssignments)
                {
                    Group group = null;
                    if (roleAssignment.Group.Name != "")
                    {
                        group = context.Web.SiteGroups.GetByName(roleAssignment.Group.Name);
                    }
                    else
                    {
                        group = GetAssociatedGroup(context, roleAssignment.Group.AssociatedGroup);
                    }

                    RoleDefinitionBindingCollection roleDefBinding = new RoleDefinitionBindingCollection(context);
                    RoleDefinition roleDef = context.Web.RoleDefinitions.GetByName(roleAssignment.PermissionLevel);
                    roleDefBinding.Add(roleDef);
                    list.RoleAssignments.Add(group, roleDefBinding);
                    context.Load(group);
                    context.Load(roleDef);
                    context.ExecuteQuery();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Break permissions of the list
        /// </summary>
        /// <param name="clientContext">Client context object</param>
        /// <param name="list">List object</param>
        /// <returns>True or false</returns>
        internal static bool BreakPermission(ClientContext clientContext, List list)
        {
            bool flag = false;
            if (null != clientContext)
            {
                try
                {
                    clientContext.Load(list, l => l.HasUniqueRoleAssignments);
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();

                    if (!list.HasUniqueRoleAssignments)
                    {
                        list.BreakRoleInheritance(false, true);
                        list.Update();
                        clientContext.Load(list);
                        clientContext.ExecuteQuery();
                    }
                }
                catch (Exception exception)
                {
                    ErrorLogger.DisplayErrorMessage(string.Format(CultureInfo.InvariantCulture, Constants.BreakingPermissionExceptionMessage, Constants.ConfigurationList, exception.Message));
                }
            }

            return flag;
        }
Example #3
0
        /// <summary>
        /// Assigns permission to group
        /// </summary>
        /// <param name="clientcontext">Client Context</param>
        /// <param name="matterCenterGroup">Group for which permission is to be assigned</param>
        /// <param name="list">List at which permission is to be assigned</param>
        /// <param name="listName">Name of the list</param>
        /// <param name="isCustomPermission">Custom permission flag</param>
        /// <returns>Status of operation</returns>
        private static bool AssignPermission(ClientContext clientcontext, Group matterCenterGroup, List list, string listName, bool isCustomPermission)
        {
            string errorFilePath = Directory.GetParent(Directory.GetCurrentDirectory()) + "/" + "ErrorLog.txt";
            bool result = false;
            try
            {
                if (null != clientcontext && null != list && null != matterCenterGroup)
                {
                    // first breaking permissions
                    Console.WriteLine("Breaking Permissions for " + listName + " list...");
                    if (!list.HasUniqueRoleAssignments)
                    {
                        list.BreakRoleInheritance(true, true);
                        list.Update();
                        clientcontext.Load(list);
                        clientcontext.ExecuteQuery();
                    }

                    //// assigning permissions
                    Principal principal = matterCenterGroup;
                    RoleDefinition role = null;
                    int permission = Convert.ToInt32(ConfigurationManager.AppSettings["Permission"], CultureInfo.InvariantCulture);
                    permission = isCustomPermission ? 3 : permission;
                    switch (permission)
                    {
                        case 0:
                            role = clientcontext.Web.RoleDefinitions.GetByType(RoleType.Contributor);
                            break;
                        case 1:
                            role = clientcontext.Web.RoleDefinitions.GetByType(RoleType.Administrator);
                            break;
                        case 3:
                            string permissionLevel = ConfigurationManager.AppSettings["MatterCenterContributePermission"];
                            RoleDefinitionCollection roles = clientcontext.Web.RoleDefinitions;
                            clientcontext.Load(roles);
                            clientcontext.ExecuteQuery();
                            role = roles.Where(item => item.Name == permissionLevel).FirstOrDefault();
                            break;
                        case 2:
                        default:
                            role = clientcontext.Web.RoleDefinitions.GetByType(RoleType.Reader);
                            break;
                    }

                    RoleDefinitionBindingCollection grpRole = new RoleDefinitionBindingCollection(clientcontext);
                    if (null != role)
                    {
                        grpRole.Add(role);
                    }
                    list.RoleAssignments.Add(principal, grpRole);
                    list.Update();
                    clientcontext.ExecuteQuery();
                    result = true;
                }
            }
            catch (Exception exception)
            {
                result = false;
                ErrorLogger.LogErrorToTextFile(errorFilePath, "Message: " + exception.Message + "\nStacktrace: " + exception.StackTrace);
            }

            return result;
        }