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:
please do specify which methods to override if at all possible. Thanks in advance for your valuable answers.
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);
}