Wednesday, December 9, 2015

Programmatically Creating SharePoint Groups with Role

private static void AddGroup(SPWeb web, SPRoleType roleType, string groupName)
    {
        var groups = web.SiteGroups;

        var userGroup = FindSiteGroup(web.Site, groupName);

        if (userGroup == null)
        {
            groups.Add(groupName, web.CurrentUser, null, string.Empty);
            web.AssociatedGroups.Add(web.SiteGroups[groupName]);
        }

        var grp = web.SiteGroups[groupName];
        grp.OnlyAllowMembersViewMembership = false;
        grp.Update();

        if (roleType != SPRoleType.None)
        {
            var asgn = new SPRoleAssignment(web.SiteGroups[groupName]);
            var roleDef = web.RoleDefinitions.GetByType(roleType);
            asgn.RoleDefinitionBindings.Add(roleDef);
            web.RoleAssignments.Add(asgn);
        }

        web.Update();
    }
__________________________________________________
SPRoleDefinition role = oweb1.RoleDefinitions[0]//Fullcontrol
SPRoleDefinition role = oweb1.RoleDefinitions[1]//design
SPRoleDefinition role = oweb1.RoleDefinitions[2]//Edit
SPRoleDefinition role = oweb1.RoleDefinitions[3]//Contribrute
SPRoleDefinition role = oweb1.RoleDefinitions[4]//Read
SPRoleDefinition role = oweb1.RoleDefinitions[5]//Limited Access
SPRoleDefinition role = oweb1.RoleDefinitions[6]//Approve
SPRoleDefinition role = oweb1.RoleDefinitions[7]//View Only
_______________________________________________________

No comments:

Post a Comment