Wednesday, 18 December 2019

CSOM : How to remove permission of all the SharePoint users?


public static void RemoveSitePermissions(string strURL)
        {
            AuthenticationManager authManagerSource = new AuthenticationManager();
            using (ClientContext clientContextSource = authManagerSource.GetWebLoginClientContext(strURL))
            {
                clientContextSource.RequestTimeout = 9999999;
                Web sourceWeb = clientContextSource.Web;
                clientContextSource.Load(sourceWeb, w => w.HasUniqueRoleAssignments, w => w.RoleDefinitions, w => w.ServerRelativeUrl, w => w.Title);
                clientContextSource.ExecuteQuery();


                if (sourceWeb.HasUniqueRoleAssignments)
                {
                    RoleAssignmentCollection webRoleAssignments = sourceWeb.RoleAssignments;
                    clientContextSource.Load(webRoleAssignments);
                    clientContextSource.ExecuteQuery();
                    Console.WriteLine("Removing permissions from web..\r\n\r\n");
                    foreach (RoleAssignment webRoleAssignment in webRoleAssignments)
                    {
                        clientContextSource.Load(webRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
                        clientContextSource.ExecuteQuery();
                        Principal oPrincipal = webRoleAssignment.Member;
                        if (oPrincipal.PrincipalType == PrincipalType.User)
                        {

                            foreach (RoleDefinition rd in webRoleAssignment.RoleDefinitionBindings)
                            {
                                try
                                {
                                    if (rd.Name != "Limited Access")
                                    {
                                        clientContextSource.Load(rd);
                                        webRoleAssignment.RoleDefinitionBindings.Remove(rd);
                                        webRoleAssignment.Update();
                                        clientContextSource.Load(webRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
                                        sourceWeb.Update();
                                        clientContextSource.ExecuteQuery();
                                        Console.Write(".");
                                    }
                                }
                                catch (Exception ex)
                                { }
                            }
                        }
                    }
                }
                ListCollection listColl = sourceWeb.Lists;
                clientContextSource.Load(listColl, lc => lc.Include(l => l.HasUniqueRoleAssignments, l => l.Hidden, l => l.Title));
                clientContextSource.ExecuteQuery();

                foreach (List list in listColl)
                {
                    if (list.HasUniqueRoleAssignments && (!list.Hidden))
                    {

                        RoleAssignmentCollection oRoleAssignments = list.RoleAssignments;
                        clientContextSource.Load(oRoleAssignments);
                        clientContextSource.ExecuteQuery();

                        Console.WriteLine("Removing permissions from lists..\r\n\r\n");
                        foreach (RoleAssignment listRoleAssignment in oRoleAssignments)
                        {
                            clientContextSource.Load(listRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
                            clientContextSource.ExecuteQuery();
                            Principal oPrincipal = listRoleAssignment.Member;
                            if (oPrincipal.PrincipalType == PrincipalType.User)
                            {
                                foreach (RoleDefinition rd in listRoleAssignment.RoleDefinitionBindings)
                                {
                                    try
                                    {
                                        if (rd.Name != "Limited Access")
                                        {
                                            clientContextSource.Load(rd);
                                            listRoleAssignment.RoleDefinitionBindings.Remove(rd);
                                            listRoleAssignment.Update();
                                            clientContextSource.Load(listRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
                                            list.Update();
                                            clientContextSource.ExecuteQuery();
                                            Console.Write(".");
                                        }
                                    }
                                    catch { }
                                }
                            }
                        }


                    }
                }
            }
        }

Reference: https://sharepoint.stackexchange.com/a/228169

No comments:

Post a Comment