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)
{
GetGroups();
}
}
//Retrieve
private void GetGroups()
{
ddlGroups.DataSource = Web.Groups;
ddlGroups.DataTextField = "Name";
ddlGroups.DataValueField = "ID";
ddlGroups.DataBind();
}
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"];
roles.RoleDefinitionBindings.Add(perms);
web.RoleAssignments.Add(roles);
// Add user to this group
SPUser user1 = web.AllUsers[@"SPDomain\Simone"];
group.AddUser(user1);
}
GetGroups();}
}
}
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.
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)
{
GetGroups();
}
}
//Retrieve
private void GetGroups()
{
ddlGroups.DataSource = Web.Groups;
ddlGroups.DataTextField = "Name";
ddlGroups.DataValueField = "ID";
ddlGroups.DataBind();
}
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"];
roles.RoleDefinitionBindings.Add(perms);
web.RoleAssignments.Add(roles);
// Add user to this group
SPUser user1 = web.AllUsers[@"SPDomain\Simone"];
group.AddUser(user1);
}
GetGroups();}
}
}
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.
0 comments:
Post a Comment