Im writing a feature to hide a document library and allow all users read only access (as taking away permissions breaks some things as im told) on activation, and the opposite on deactivation.

Permissions are not my strong suit, so im wondering if anyone has any advice? I tried googling this issue, but either I dont understand the examples or theyre not doing what I need them to do.

I know im supposed to iterate through the RoleAssignments collection in the SPWeb object, but im not sure what im supposed to do with the groups or users that I can access through that. Do I delete all groups and reasssign them all to a "read-only" group? Or is there a way to lower the permissions of everyone?

Also, on the topic of reversing this, Is there a way of resetting this? Or would the site owner be responsible for fixing everyones permissions after the feature is deactivated?


  • You would need to store the current permissions if you wanted to revert back on deactivation. This may be complicated if the users no longer exist, have changed, etc. You could always set it back to inherit. – shufler Mar 26 '12 at 22:29
  • It would appear I didnt understand inheritance of permissions. I thought the doclib would inherit from its SPWeb's parents document library. Reinheriting is probably the best option. Thanks. – John S Mar 26 '12 at 22:33

You can do something like this

                SPWeb web = site.RootWeb;
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Test1"];

                SPUser allusers = web.EnsureUser("NT AUTHORITY\\authenticated users");
                SPRoleAssignment role = new SPRoleAssignment(allusers);
                SPRoleDefinition def = web.RoleDefinitions.GetByType(SPRoleType.Reader);

                if (!list.HasUniqueRoleAssignments)

                web.AllowUnsafeUpdates = false;

To reset permissions use


Also you need to take into consideration, if the list already has UniqueRoleAssignments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.