Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

how to add the group and user in sharepoint in Programmatically

thank you.
Posted

C#
private void AddGroupAndUsers()
        { 
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            web.SiteGroups.Add("MyTestGroup", web.CurrentUser, web.CurrentUser, string.Empty);

            //Then you probably want to give some permissions to group (let’s try to provide contributor role)

            SPGroup group = web.SiteGroups["MyTestGroup"];
            SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
            SPRoleAssignment roleAssigment = new SPRoleAssignment(group);
            roleAssigment.RoleDefinitionBindings.Add(roleDefinition);
            web.RoleAssignments.Add(roleAssigment);
            web.Update();

           // Now you can add some users to your new group:

            SPUser spUser = web.EnsureUser("server\\username");
            group.Users.Add(spUser.LoginName, spUser.Email, spUser.Name, spUser.Notes);
            web.AllowUnsafeUpdates = false;

}
 
Share this answer
 
v3
Select ListSeettings>Permissions>AddUserOrGroup To List
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900