Add Retrieve Groups in Sharepoint 2010 using object model

In this post I will walk you through an example where we have created an application page with a TextBox(to enter group name),a Button(to create a group) and a drop-down list to list all the groups in the current site collection.

1. Create a new Project in VS 2010.
2. Select Application page as a template.
3. Add the below controls to your application page.

<asp:TextBox runat="server" ID="txtGroup" />
<asp:Button runat="server" ID="btnAddGroup" Text="Add Group" OnClick="btnAddGroups_Click" />
<br />
Existing Groups:
<asp:DropDownList runat="server" ID="ddlGroups" />

4. Add the below code in your Code behind of the application page.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace GroupManagement
public partial class AddGroup : LayoutsPageBase
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
private void GetGroups()
ddlGroups.DataSource = Web.Groups;
ddlGroups.DataTextField = "Name";
ddlGroups.DataValueField = "ID";

protected void btnAddGroups_Click(object sender, EventArgs e)
// Retrieve root collection
SPSite site = new SPSite("http://SpSite");
using (SPWeb web = site.OpenWeb())
string newGroup = txtGroup.Text;
// Create
web.SiteGroups.Add(newGroup, web.CurrentUser, web.CurrentUser, "Created this group via object model");
// Assign to site
SPGroup group = web.SiteGroups[newGroup];
SPRoleAssignment roles = new SPRoleAssignment(group);
SPRoleDefinition perms = web.RoleDefinitions["Full Control"];
// Add user to this group
SPUser user1 = web.AllUsers[@"SPDomain\Simone"];
In the above code, the group gets created in Button’s click event. The current user becomes the default member and the group's owner. Then the the Full Control permission is assigned to the group.


Post a Comment


This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at .

Copyright (c) 2010 @ All rights are reserved.Do Not Copy.

@ Learning