How to assign an exiting SharePoint group to a site using the SharePoint object model?

The below code demonstrates how you can assign a SharePoint Group to a SharePoint site  using the SharePoint object model.

SPSite spsite = new SPSite("Site_Name");
try
{
     using (SPWeb web = spsite.OpenWeb())
     {
          web.AllowUnsafeUpdates = true;
          SPGroup sgrp = web.SiteGroups["Group_Name"];
          SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
          SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)sgrp);
          roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
          web.RoleAssignments.Add(roleAssignment);
          web.Update();
     }
}
catch (Exception ex)
{
     LoggingManager.WriteEventToLogFile(ex);
}

 


 

Print | posted @ Friday, January 21, 2011 1:24 PM

Comments on this entry:

No comments posted yet.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: