Group operation: create, grant permission and add member using client object model
public void Main(string[] args)
{
var siteCollectionUrl = "http://your_site_url";
var member = "domain\\account";
using (var ctx = new ClientContext(siteCollectionUrl))
{
var web = ctx.Web;
ctx.Load(web, w => w.ServerRelativeUrl);
ctx.ExecuteQuery();
// Create a new group named 'Contact'
var groupInfo = new GroupCreationInformation();
groupInfo.Title = "Contact";
groupInfo.Description = string.Format(@"Use this group to grant people full control permissions to the SharePoint site: {0}", groupInfo.Title);
var group = web.SiteGroups.Add(groupInfo);
ctx.Load(group);
ctx.ExecuteQuery();
// Set group properties
group.OnlyAllowMembersViewMembership = false;
group.AllowMembersEditMembership = true;
group.Update();
ctx.ExecuteQuery();
// Grant group permission
var fullControlPermission = web.RoleDefinitions.GetByName("Full Control");
var roleBinding = new RoleDefinitionBindingCollection(ctx);
roleBinding.Add(fullControlPermission);
web.RoleAssignments.Add(group, roleBinding);
ctx.ExecuteQuery();
// Add new member to group
var u = web.EnsureUser(member);
ctx.Load(u);
ctx.ExecuteQuery();
var userInfo = new UserCreationInformation();
userInfo.Email = u.Email;
userInfo.Title = u.Title;
userInfo.LoginName = u.LoginName;
group.Users.Add(userInfo);
ctx.ExecuteQuery();
}
}