简体   繁体   中英

How to remove user permissions from a SharePoint 2013 document library using c#

I've seen many ways to remove groups from a document library in SharePoint 2013. But what I need to do is to remove an individual's permissions to a document library and not a group.

This is the code I've been using. It runs without error, but it fails to remove the users permission on the document library.

I do not want to remove the user from any groups or from the site.

    public void DeleteUserFromDocumentLibrary(string fullUserLogon, string shareName)
    {
        string Myurl = "https://host.domainname.com/fs";

        using (SPSite site = new SPSite(Myurl))
        {
            site.AllowUnsafeUpdates = true;

            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;
                web.ValidateFormDigest();

                fullUserLogon = "i:0#.w|" + fullUserLogon;
                SPPrincipal principal = web.EnsureUser(fullUserLogon);
                SPRoleAssignment rAssignment = new SPRoleAssignment(principal);
                SPList list = web.Lists[shareName];

                foreach (SPListItem item in list.Items)
                {
                    item.BreakRoleInheritance(true);
                    item.RoleAssignments.Remove(principal);
                    item.Update();
                }


               web.AllowUnsafeUpdates = false;
            }
            site.AllowUnsafeUpdates = false;
        }
    }

Thanks

what you are doing is break inheritance and remove a user permission for each item on your document library. The permissions on the library itself will stay the same. You are in fact setting item level permissions.

Just try and do the same actions on the list object, and remove the foreach through the list items.

 list.BreakRoleInheritance(true);
 list.RoleAssignments.Remove(principal);
 list.Update();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM