create roles and permission in sharepoint programmatically

Advertisement

In this article I will explain how to create roles and permissions using code..

In Feature Activated I am writing this code


public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web1 = site.OpenWeb())
                    {
                        web1.SiteGroups.Add("SharePoint Group", web1.CurrentUser, web1.CurrentUser, "This is the Group created by SharePoint Group.");
                        web1.AssociatedGroups.Add(web1.SiteGroups["SharePoint Group"]);
                        web1.Update();
                        SPRoleAssignment assignment = new SPRoleAssignment(web1.SiteGroups["SharePoint Group"]);
                        SPRoleDefinition _role = web1.RoleDefinitions["Contribute"];
                        assignment.RoleDefinitionBindings.Add(_role);
                        web1.RoleAssignments.Add(assignment);

                        web1.SiteGroups.Add("DotNet Group", web1.CurrentUser, web1.CurrentUser, "This is the Group created by DotNet Group.");
                        web1.AssociatedGroups.Add(web1.SiteGroups["DotNet Group"]);
                        web1.Update();
                        SPRoleAssignment assignment1 = new SPRoleAssignment(web1.SiteGroups["DotNet Group"]);
                        SPRoleDefinition _role1 = web1.RoleDefinitions["Approve"];
                        assignment1.RoleDefinitionBindings.Add(_role1);
                        web1.RoleAssignments.Add(assignment1);


                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Once we deploy.. we can able to see these roles and permissions in SitePermissions..
Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment