Monday, October 25, 2010

Create a User Group using Client Object Model

//get the connection

ClientContext ctx = new ClientContext(“http://SPSite”);

//create the group

GroupCreationInformation grp = new GroupCreationInformation();

grp .Title = “Your New Group”;

grp .Description = “This is a custom group created using the client object model”;

//add it to the list of site groups

Group newgrp = ctx.Web.SiteGroups.Add(grp);

//Get a role.

RoleDefinition rd = ctx.Web.RoleDefinitions.GetByName(“Permission level Name”); // – > To
Create a Custom Role definition or Permission level see my post Here

//create the role definition binding collection

RoleDefinitionBindingCollection rdb = new RoleDefinitionBindingCollection(ctx);

//add the role definition to the collection

rdb.Add(rd);

//create a RoleAssigment with the group and role definition

ctx.Web.RoleAssignments.Add(grp, rdb);

//execute the query to add everything

ctx.ExecuteQuery();

No comments:

Post a Comment