/// <summary> /// Breaks permission for user /// </summary> /// <param name="clientContext">Client context</param> /// <param name="matterName">Matter name</param> /// <param name="copyPermissionsFromParent">Copy permissions from parent</param> /// <param name="calendarName">Calendar name</param> /// <returns>Boolean value</returns> internal static bool BreakPermission(ClientContext clientContext, string matterName, bool copyPermissionsFromParent, string calendarName = null) { bool flag = false; try { Microsoft.SharePoint.Client.Web web = clientContext.Web; string matterOrCalendar = !string.IsNullOrWhiteSpace(calendarName) ? calendarName : matterName; List list = web.Lists.GetByTitle(matterOrCalendar); clientContext.Load(list, l => l.HasUniqueRoleAssignments); clientContext.ExecuteQuery(); if (!list.HasUniqueRoleAssignments) { list.BreakRoleInheritance(copyPermissionsFromParent, true); list.Update(); clientContext.Load(list); clientContext.ExecuteQuery(); flag = true; } } catch (Exception exception) { Utility.DisplayAndLogError(Utility.ErrorFilePath, "Message: " + "Matter name: " + exception.Message + matterName + "\nStacktrace: " + exception.StackTrace); throw; } return(flag); }
public static void AsignarPermiso(ClientContext c) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Asignando Permisos a Pedido."); List pedidos = c.Web.Lists.GetByTitle("Pedidos"); RoleDefinitionBindingCollection integrantesPedidos = new RoleDefinitionBindingCollection(c); RoleDefinition colaborarSinEliminar = c.Web.RoleDefinitions.GetByName("Colaborar sin eliminar"); Group integrantes = c.Web.SiteGroups.GetByName("Integrantes"); /////////////////////////////////////////////// integrantesPedidos.Add(colaborarSinEliminar); pedidos.BreakRoleInheritance(false, false); c.Load(pedidos.RoleAssignments.Add(integrantes, integrantesPedidos)); pedidos.Update(); c.ExecuteQuery(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(" Asiganado"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Asignando Permisos a Pedido Detalle."); List pedidosDetalles = c.Web.Lists.GetByTitle("Pedido Detalle"); RoleDefinitionBindingCollection integrantesPedidosD = new RoleDefinitionBindingCollection(c); integrantesPedidos.Add(colaborarSinEliminar); pedidosDetalles.BreakRoleInheritance(false, false); c.Load(pedidosDetalles.RoleAssignments.Add(integrantes, integrantesPedidos)); pedidosDetalles.Update(); c.ExecuteQuery(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(" Asiganado"); }
/// <summary> /// Breaks the permissions of the list. /// </summary> /// <param name="clientContext">Client context</param> /// <param name="libraryName">Name of list</param> /// <param name="isCopyRoleAssignment">Flag to copy permission from parent</param> /// <returns>Success flag</returns> public static bool BreakPermission(ClientContext clientContext, string libraryName, bool isCopyRoleAssignment) { bool flag = false; if (null != clientContext && !string.IsNullOrWhiteSpace(libraryName)) { try { List list = clientContext.Web.Lists.GetByTitle(libraryName); clientContext.Load(list, l => l.HasUniqueRoleAssignments); clientContext.ExecuteQuery(); if (!list.HasUniqueRoleAssignments) { list.BreakRoleInheritance(isCopyRoleAssignment, true); list.Update(); clientContext.Load(list); clientContext.ExecuteQuery(); flag = true; } } catch (Exception) { throw; // This will transfer control to catch block of parent function. } } return(flag); }
/// <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); }
public static void BreakRoleInheritanceAndRemoveRoles(this List value, bool copyRoleAssignments, bool clearSubscopes, bool removeRoleAssignments) { value.BreakRoleInheritance(copyRoleAssignments, clearSubscopes); if (removeRoleAssignments) { value.RoleAssignments.ToList().ForEach(ra => ra.DeleteObject()); } }
private void SetListPermissions(List list) { if (!list.HasUniqueRoleAssignments || !_settings.Value.Force) { var membersGroup = list.ParentWeb.AssociatedMemberGroup; if (membersGroup == null) { _logger.LogWarning($"Members Group not found on {list.DefaultViewUrl}"); return; } _logger.LogDebug($"Granting Members Group Read to {list.DefaultViewUrl}"); // ensure list perms are broken list.BreakRoleInheritance(true, true); // add "read" perms to the members group { var roleDefinition = new RoleDefinitionBindingCollection(list.Context) { list.ParentWeb.RoleDefinitions.GetByName("Read") }; list.Context.Load(list.RoleAssignments.Add(membersGroup, roleDefinition)); list.Context.ExecuteQueryRetry(); } _logger.LogDebug($"Removing Members Edit rights from {list.DefaultViewUrl}"); // remove "edit" perms from the members group { var roleAssignment = list.RoleAssignments.GetByPrincipal(membersGroup); var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings; list.Context.Load(roleDefinitionBindings); list.Context.ExecuteQueryRetry(); foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == "Edit")) { roleDefinitionBindings.Remove(roleDefinition); roleAssignment.Update(); list.Context.ExecuteQueryRetry(); break; } } } else { _logger.LogDebug($"HasUniqueRoleAssignments == true for {list.DefaultViewUrl}"); } }
public void BreakRoleInheritanceForList(string title, string password) { var clientContext = authentication.Credentials(password); clientContext.Load(clientContext.Web, a => a.Lists); clientContext.ExecuteQuery(); List list = clientContext.Web.Lists.GetByTitle(title); //Stop Inheritance from parent list.BreakRoleInheritance(false, false); list.Update(); clientContext.ExecuteQuery(); }
//gavdcodeend 10 //gavdcodebegin 11 static void SpCsCsomBreakSecurityInheritanceList(ClientContext spCtx) { Web myWeb = spCtx.Web; List myList = myWeb.Lists.GetByTitle("NewListCsCsom"); spCtx.Load(myList, hura => hura.HasUniqueRoleAssignments); spCtx.ExecuteQuery(); if (myList.HasUniqueRoleAssignments == false) { myList.BreakRoleInheritance(false, true); } myList.Update(); spCtx.ExecuteQuery(); }
private void Clear(ClientContext clientContext, List list) { if (!list.HasUniqueRoleAssignments) { list.BreakRoleInheritance(false, true); } //else //{ // for (var index = list.RoleAssignments.Count - 1; index >= 0; index--) // { // //list.RoleAssignments[index].DeleteObject(); // list.RoleAssignments.GetByPrincipal(list.RoleAssignments[index].Member).DeleteObject(); // } //} }
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(); } } }
public static async Task SetRoles(this List list, params Role[] Roles) { var clientContext = list.Context.AsClientContext(); list.BreakRoleInheritance(false, true); list.Update(); await clientContext.ExecuteQueryAsync(); var listRoles = list.RoleAssignments; clientContext.Load(listRoles); await clientContext.ExecuteQueryAsync(); if (listRoles.Count > 0) { for (var counter = listRoles.Count - 1; counter > -1; counter--) { listRoles[counter].DeleteObject(); await clientContext.ExecuteQueryAsync(); } } foreach (var role in Roles) { var group = await clientContext.GetGroup(role.GroupName); var roletype = clientContext.Web.RoleDefinitions.GetByType(role.RoleType); clientContext.Load(roletype); await clientContext.ExecuteQueryAsync(); var collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext) { roletype }; listRoles.Add(group, collRoleDefinitionBinding); await clientContext.ExecuteQueryAsync(); } }
public override void BreakRoleInheritance(bool copyRoleDefinitions) { _list.BreakRoleInheritance(copyRoleDefinitions, false); _context.ExecuteQuery(); }
/// <summary> /// Updates permissions on existing lists /// </summary> /// <param name="groupName">Group name</param> /// <param name="clientContext">Client context</param> private static void UpdateListPermissions(string groupName, ClientContext clientContext) { try { string permissionLevelName = ConfigurationManager.AppSettings["MatterCenterContributePermission"]; string permissionLevelDescription = ConfigurationManager.AppSettings["MatterCenterContributePermissionDescription"]; if (CreateNewPermissionLevel(clientContext, permissionLevelName, permissionLevelDescription)) { ErrorMessage.ShowMessage(ConfigurationManager.AppSettings["UpdateList"], ErrorMessage.MessageType.Notification); string[] lists = ConfigurationManager.AppSettings["ListTitles"].Split(','); int listCount = lists.Count(); for (int iterator = 0; iterator < listCount; iterator++) { string listName = lists[iterator]; List list = GetListIfExists(clientContext, listName); // Check if list already exists if (null != list) { if (!list.HasUniqueRoleAssignments) { list.BreakRoleInheritance(true, true); ErrorMessage.ShowMessage(string.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["RemovedPermission"], listName), ErrorMessage.MessageType.Notification); clientContext.ExecuteQuery(); } // remove group (Matter Center Users) from list RoleAssignmentCollection collection = list.RoleAssignments; clientContext.Load(collection, items => items.Include(item => item.Member.Title).Where(item => item.Member.Title == groupName)); clientContext.ExecuteQuery(); // Check if group is exists in list, then only remove it if (0 < collection.Count()) { collection.FirstOrDefault().DeleteObject(); clientContext.ExecuteQuery(); ErrorMessage.ShowMessage(string.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["RemovedGroup"], groupName, listName), ErrorMessage.MessageType.Notification); } else { ErrorMessage.ShowMessage(string.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["GroupFailure"], groupName, listName), ErrorMessage.MessageType.Notification); } // Assign new permission to group(Matter Center Users) & add that group to list // 1. select new permission level RoleDefinition matterCenterContribute = GetPermissionLevel(clientContext, permissionLevelName); // 2. select Group (Matter Center Users) RoleAssignment group = GetGroup(clientContext, groupName); // 3. Check if permission level already added to group, if not then assign new permission level(Matter Center Contribute) to group AssignPermissionLevelToGroup(clientContext, matterCenterContribute, group); // 4. Add group into list AddGroupToList(groupName, clientContext, list, matterCenterContribute); ErrorMessage.ShowMessage(string.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["AddGroup"], group.Member.Title, listName), ErrorMessage.MessageType.Notification); // 5. get list item and User who created it and assign full control to that user AssignFullControltoListItem(clientContext, list); } else { ErrorMessage.ShowMessage(string.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["ListNotFound"], listName), ErrorMessage.MessageType.Error); } } } else { ErrorMessage.ShowMessage(ConfigurationManager.AppSettings["PermissionFailure"], ErrorMessage.MessageType.Error); } } catch (Exception exception) { ErrorLogger.LogErrorToTextFile(errorFilePath, string.Concat(exception.Message, "\n", exception.StackTrace)); } }
/// <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; }
/// <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; }
/// <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); }
public void BreakRoleInheritance(bool copyRoleAssignments, bool clearScopes) { _list.BreakRoleInheritance(copyRoleAssignments, clearScopes); _list.Context.ExecuteQuery(); Refresh(); }