0

I want to set read permissions to some sharepoint groups to a list.

For existing sites I did it manually, Stop Inherit permissions. Then on groups I chose the groups and edited their permissions, which I set to Read only.

I want to do exactly the same but by using code. So when a new site is provisioned permissions are set this way by default.

private void SetReadAccessOnList(SPSite newSite, string groupName)
        {
            try
            {
                SPList WorkspaceAccessList = newSite.RootWeb.Lists["mylist"];
                newSite.RootWeb.AllowUnsafeUpdates = true;
                WorkspaceAccessList.BreakRoleInheritance(true);                                
                using (SPWeb webSite = newSite.OpenWeb())
                {
                    SPGroup group = webSite.Groups[groupName];
                    AssignReadPermissionsToList(webSite, WorkspaceAccessList, group, SPRoleType.Reader);
                }                   
                newSite.RootWeb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Error Site: :  {0} || {1}", ex.Message, ex.StackTrace);
                SPUtility.TransferToErrorPage(ex.ToString());
            }           
        }


        public static void AssignReadPermissionsToList(SPWeb web, SPSecurableObject secObj, SPPrincipal obj, SPRoleType roleType)
        {
            if (!secObj.HasUniqueRoleAssignments)
            {
                secObj.BreakRoleInheritance(false, true);
            }
            SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
            SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            secObj.RoleAssignments.Add(roleAssignment);
        }

Thanks.

2 Answers 2

4

You can set it like this:

public static void AssignPermissionsToItem(SPWeb web, SPSecurableObject secObj, SPPrincipal obj, SPRoleType roleType)
{
    if (!secObj.HasUniqueRoleAssignments)
    {
        secObj.BreakRoleInheritance(false, true);
    }

    SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
    SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

    secObj.RoleAssignments.Add(roleAssignment);
}

secObj - SPList, SPListItem

obj - SPUser, SPGroup

About SPRoleType you can read here.

3
  • please check my updated code, will that make the trick or I am missing something else?+ Mar 19, 2012 at 14:37
  • Remove WorkspaceAccessList.BreakRoleInheritance(true); from your code.
    – Alexander
    Mar 19, 2012 at 14:44
  • how to get/set the spsecuruable object into a SPLIST?please help
    – samolpp2
    Feb 22, 2017 at 14:56
1

You will need to add the permissions code to the WebProvisioned Event Reciever:

http://blog.qumsieh.ca/2010/10/07/how-to-attach-an-event-receiver-to-the-web-added-event-and-for-what-purpose/

And here is the code with which you can set custom permissions programmatically to a list or document library:

http://msdn.microsoft.com/en-us/library/ee535228.aspx

2
  • please check my updated code, will that make the trick or I am missing something else? Mar 19, 2012 at 14:36
  • Yeah both functions should do the trick. If you get any errors, you can post them here. Mar 19, 2012 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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