0

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?

Thanks!

2
  • 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
    Commented Mar 26, 2012 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.
    – user5907
    Commented Mar 26, 2012 at 22:33

1 Answer 1

0

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);
                role.RoleDefinitionBindings.Add(def);

                if (!list.HasUniqueRoleAssignments)
                {
                    list.BreakRoleInheritance(false);
                }
                list.RoleAssignments.Add(role);
                list.Update();

                web.AllowUnsafeUpdates = false;

To reset permissions use

            list.ResetRoleInheritance();
            list.Update();

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 and acknowledge you have read our privacy policy.