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

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.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
please check my updated code, will that make the trick or I am missing something else?+ –  L.V. Sharepoint Architect Mar 19 '12 at 14:37
    
Remove WorkspaceAccessList.BreakRoleInheritance(true); from your code. –  Alexander Mar 19 '12 at 14:44
add comment

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

share|improve this answer
    
please check my updated code, will that make the trick or I am missing something else? –  L.V. Sharepoint Architect Mar 19 '12 at 14:36
    
Yeah both functions should do the trick. If you get any errors, you can post them here. –  Vardhaman Deshpande Mar 19 '12 at 14:46
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.