Sree Kuttan
Sree Kuttan

Reputation: 203

Can we set custom permission of a list using features in sharepoint 2010?

I am working on SharePoint 2010 . Creating features to automate the custom list creation. I am assigned a task to automatically set the custom permission of each list using features programatically. I would like to know if it is possible and if yes how.

There are 2 things here:

  1. Custom permission level is to be created with contribute-delete permissions.
  2. This permission and default available permissions are to be set to lists which are custom made using features.

please do specify which methods to override if at all possible. Thanks in advance for your valuable answers.

Upvotes: 2

Views: 1415

Answers (1)

Aquila Sands
Aquila Sands

Reputation: 1471

Override the FeatureActivated method of your feature receiver with code similar to the following:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = (SPWeb)properties.Feature.Parent;
        SPList list = web.GetList(web.Url + "/Lists/MyList");

        SPRoleDefinition roleDef = new SPRoleDefinition();
        roleDef.BasePermissions = SPBasePermissions.DeleteListItems 
            | SPBasePermissions.AddListItems 
            | SPBasePermissions.EditListItems;
        roleDef.Description = "Custom permissions deployed by feature";
        roleDef.Name = "Contribute-Delete";
        web.RoleDefinitions.Add(roleDef);
        roleDef = web.RoleDefinitions[roleDef.Name];
        SPMember owner = web.SiteUsers[@"Domain\username"];
        SPUser user = web.SiteUsers[@"Domain\username"];
        web.SiteGroups.Add("Contribute-Delete",owner, user,"A group for contribute delete access");
        SPGroup ContributeDeleteGroup = web.SiteGroups["Contribute-Delete"];
        SPRoleAssignment roleAssignment = new SPRoleAssignment(ContributeDeleteGroup);
        roleAssignment.RoleDefinitionBindings.Add(roleDef);
        list.BreakRoleInheritance(true);
        list.RoleAssignments.Add(ContributeDeleteGroup);
    }

Upvotes: 3

Related Questions