Subscribe:

Labels

Wednesday, August 17, 2011

Programatically Create User Groups using Client object model SharePoint 2010

Here is the Code snippet for Creating a Group and assigning it a permission level or role  using Client Object model . To add the users to this group using Client OM see this Post


Create a User Group –
//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”); // 
//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