So in Sharepoint there are 2 ways to see the groups, site settings, people and groups (I see my AD groups there twice), or site setttings permissions, I see the group there only once.
All I need is to add an AD Group into an existing SP group with the contribute permission level.
The code below works fine but the questions are the following1
Why the ad group appears 2 times in People and Groups? how can I fix that? why adding the ad group to an sp group is not enough? you still have to add a permission level to the ad group. If I dont do the ensureuser, then the roleaassignments.add fails with user not found
string xlosgroupnamewithdomain = string.Concat(domainName, @"\", xlosgroupname);
SPUser adLosGroup = web.EnsureUser(xlosgroupnamewithdomain);
membersGroup.AddUser(adLosGroup);
SPRoleDefinition contribute = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
SPRoleAssignment roleAssignment = new SPRoleAssignment(xlosgroupnamewithdomain, string.Empty, string.Empty, string.Empty);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
roleDefBindings.Add(contribute);
roleAssignments.Add(roleAssignment);
EDIT:
When I try to do this, it says the user does not exist. in the adduser line
string xlosgroupnamewithdomain = string.Concat(domainName, @"\", xlosgroupname);
web.EnsureUser(xlosgroupnamewithdomain);
var adLosGroup = web.Users[xlosgroupnamewithdomain];
membersGroup.AddUser(adLosGroup);