none
how to assign permissions to SharePoint group through programmatically? RRS feed

  • Question

  • hi all,

    i created one sharepoint group programatically by using the following code:

    SPSecurity

     

    .RunWithElevatedPrivileges(delegate

    {

     

    SPSite oSite = SPContext.Current.Site;

     

    SPWeb oWeb = oSite.OpenWeb();

    oWeb.AllowUnsafeUpdates =

    true;

     

     

    SPUser owner = oWeb.EnsureUser(oWeb.CurrentUser.LoginName);

     

     

    SPUser oUser = oWeb.EnsureUser(oWeb.CurrentUser.LoginName);

    oWeb.SiteGroups.Add(

    "CustomGroup", owner, oUser, "custom group");

     

     

     

    oWeb.Update();

    oWeb.AllowUnsafeUpdates =

    false;

    });

    here the group is creating but this group doesn't have permissions. i want to assign permission as 'Contribute' . so how we can give permissions to SharePoint group programmatically.

    thanks in advance.........

     

    Thursday, July 29, 2010 11:59 AM

Answers

  • As I said, it depends on the securable object that you are setting the permissions for.  The code above is to do it for the root web site where all permissions are set to be inherited from by default.  If you want to do it on a sub site then you need to break security inheritance first.   In that case you need to call the BreakRoleInheritance(true) method on the securable object first.  The boolean tells it to copy the parent's existing permissions.
    Paul Stork SharePoint Server MVP
    • Marked as answer by Lily Wu Friday, August 6, 2010 2:27 AM
    Thursday, July 29, 2010 3:50 PM

All replies

  • Permissions are assigned to a specific secuarable object.  So to assign contribute permission to the group you need to know in what context you are assigning the permissions.  So let's say you want to assign the permissions to the web site where you create the group.  Something like the following code is what you need.

              //Create a new assignment for the Web site group where principal is the group you created.
              SPRoleAssignment assignment = new SPRoleAssignment(principal);

              //Get the role definitions binding collection for the new assignment object
              SPRoleDefinitionBindingCollection definitions =
                assignment.RoleDefinitionBindings;

              //Add the "Contribute" role definition where roleDefinitions is the RoleDefinitions property for the web site root.
              //to the role assignment
              definitions.Add(roleDefinitions["Contribute"]);

              //Add the role assignment to the webs role assignment collection of the group
              roleAssignments.Add(assignment);


    Paul Stork SharePoint Server MVP
    Thursday, July 29, 2010 12:26 PM
  • thnaks Paul for your reply,

    i excuted your code but i'm getting "This operation is not allowed on an object that inherits permissions" this error. Please tell me how we can remove this error...

    thanks in advance.........

    Thursday, July 29, 2010 12:47 PM
  • As I said, it depends on the securable object that you are setting the permissions for.  The code above is to do it for the root web site where all permissions are set to be inherited from by default.  If you want to do it on a sub site then you need to break security inheritance first.   In that case you need to call the BreakRoleInheritance(true) method on the securable object first.  The boolean tells it to copy the parent's existing permissions.
    Paul Stork SharePoint Server MVP
    • Marked as answer by Lily Wu Friday, August 6, 2010 2:27 AM
    Thursday, July 29, 2010 3:50 PM