Sep 19, 2009

Creating custom groups and permissions associated with the groups

Here i m going to create the custom groups,custom permission level and association or binding between the group and permission level.

So first we are going to create the custom group

---------------------------------------
SPSite site = new SPSite("http://spvm:2409");

SPWeb oSubWeb = site.OpenWeb();

oSubWeb.AllowUnsafeUpdates = true;


oSubWeb.AllowUnsafeUpdates = true;

//Here get the owner group from the site...to get the other groups like member or visitor group follow the earlier post Click here.

SPGroup owner = oSubWeb.SiteGroups.GetByID(int.Parse(oSubWeb.Properties["vti_associateownergroup"]));

oSubWeb.SiteGroups.Add("Customgroups", owner, null, "Custom groups");

oSubWeb.Update();


---------------------------------------------------------------

Once the group is created we will create the custom permission level..

---------------------------------------------------------------
SPRoleDefinition role = new SPRoleDefinition();

role.Name = "CustomPermission";

role.Description = "Custom permission level to View,Edit,Add list items";

role.BasePermissions = SPBasePermissions.OpenItems

| SPBasePermissions.ViewVersions

| SPBasePermissions.ViewFormPages

| SPBasePermissions.Open

| SPBasePermissions.ViewPages

| SPBasePermissions.UseClientIntegration

| SPBasePermissions.UseRemoteAPIs

| SPBasePermissions.CreateAlerts

| SPBasePermissions.ViewListItems

| SPBasePermissions.ViewPages

| SPBasePermissions.EditListItems

| SPBasePermissions.AddListItems

| SPBasePermissions.DeleteListItems;

oSubWeb.AllowUnsafeUpdates = true;

oSubWeb.RoleDefinitions.Add(role);

---------------------------------------------------------------

Once the custom permission level is create ,now its time to bind this permission level to custom created group

----------------------------------------------------------------
//Name of the custom group created above

SPGroup oGroup = oSubWeb.SiteGroups["Customgroups"];

oSubWeb.AllowUnsafeUpdates = true;

SPRoleAssignment roleAssignment = new SPRoleAssignment(oGroup);

//Name of the role definition created above roleAssignment.RoleDefinitionBindings.Add(oSubWeb.RoleDefinitions["CustomPermission"]);

oSubWeb.AllowUnsafeUpdates = true;

oSubWeb.RoleAssignments.Add(roleAssignment);

oSubWeb.AllowUnsafeUpdates = true;</div>
oSubWeb.Update();

oSubWeb.AllowUnsafeUpdates = false;

---------------------------------------------------------------------

So your custom group with custom permission level is ready to use..

Enjoy!!!

No comments:

Post a Comment