Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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