Saturday, January 12, 2013

Create and Add user to Groups in SharePoint using Object model

In this post we will discuss how to create a SharePoint group and how to add users to the particular group using SharePoint object model.

You can also check my previous posts on ECMA Client Object Model (SP.js) in SharePoint 2010 [Read here], Client Object Model SharePoint 2010 [Read here], Create list using object model in SharePoint [Read here].

Below is the full code:

SPSite site = new SPSite("http://Site URL");

using (SPWeb web = site.OpenWeb())
string newGroup = "MyGroup";

web.SiteGroups.Add(newGroup, web.CurrentUser, web.CurrentUser,"This is our new group");

SPGroup group = web.SiteGroups[newGroup];

SPRoleAssignment roles = new SPRoleAssignment(group);

SPRoleDefinition permission = web.RoleDefinitions["Full Control"]; //Here the group will have Full Control permission level.



// Add users to this group
SPUser newUser = web.AllUsers[@"Administratoe\Fewlines4Biju"];


The above code will create a group and add a new user to the group.

No comments:

Post a Comment