How to Add Users to a Group in Sharepoint 2010
Code snippets for how to add users in groups in various object models of Sharepoint.
Server Object Model -
Adding users to your new group -
SPUser spUser = spWeb.EnsureUser("domain\isha");
if (spUser != null)
{
SPGroup spGroup = spWeb.Groups["Mygroup"];
if (spGroup != null)
spGroup.AddUser(spUser);
}
Using Silverlight Client Object model -
ClientContext ctx = new ClientContext("http://SPsite");
Group membersGroup = ctx.Web.AssociatedMemberGroup;
// Add existing user to membersGroup
User currentUser = membersGroup.Users.AddUser(ctx.Web.CurrentUser);
// Add new user to membersGroup
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = "isha@Learningsharepoint.com";
userCreationInfo.LoginName = @"domain\isha";
userCreationInfo.Title = "Isha Attlee";
User newUser = membersGroup.Users.Add(userCreationInfo);
.Net Managed Client Object model -
ClientContext ctx = new ClientContext(“http://SPSite”);
//get the group
Group grp = ctx.Web.SiteGroups.GetById(GroupId);
//create the user object
UserCreationInformation usr = new UserCreationInformation();
usr.LoginName = “Domain\isha”;
usr.Email = “ikapoo@Oursite.com”;
usr.Title = “Programmer”;
//add it to the group
grp.Users.Add(usr);
//execute the query to add the user
ctx.ExecuteQuery();
Server Object Model -
Adding users to your new group -
SPUser spUser = spWeb.EnsureUser("domain\isha");
if (spUser != null)
{
SPGroup spGroup = spWeb.Groups["Mygroup"];
if (spGroup != null)
spGroup.AddUser(spUser);
}
Using Silverlight Client Object model -
ClientContext ctx = new ClientContext("http://SPsite");
Group membersGroup = ctx.Web.AssociatedMemberGroup;
// Add existing user to membersGroup
User currentUser = membersGroup.Users.AddUser(ctx.Web.CurrentUser);
// Add new user to membersGroup
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = "isha@Learningsharepoint.com";
userCreationInfo.LoginName = @"domain\isha";
userCreationInfo.Title = "Isha Attlee";
User newUser = membersGroup.Users.Add(userCreationInfo);
.Net Managed Client Object model -
ClientContext ctx = new ClientContext(“http://SPSite”);
//get the group
Group grp = ctx.Web.SiteGroups.GetById(GroupId);
//create the user object
UserCreationInformation usr = new UserCreationInformation();
usr.LoginName = “Domain\isha”;
usr.Email = “ikapoo@Oursite.com”;
usr.Title = “Programmer”;
//add it to the group
grp.Users.Add(usr);
//execute the query to add the user
ctx.ExecuteQuery();
0 comments:
Post a Comment