1

I am quite new to SharePoint, what I am trying to do is resolveing everything into users (sp group, User, AD group, Distribution List etc.) I am using the SPUtility method "GetPrincipalsInGroup" which works o to some extent but this method does not really reutrn me every user (as I am in Domain DE I can see the users in Germany but there is a users from US which I cannot see in my results it misses in the results: Please suggest how I can get over this issue: Below is the code I am doing:

SPRoleAssignmentCollection roleassignment = e.Web.RoleAssignments;

            foreach (SPRoleAssignment siterole in roleassignment)
            {
                SPPrincipal principal = siterole.Member;
                List<SPUser> user = new List<SPUser>();
                ResolveGroup(e.Web, principal, user);

            }

private void ResolveGroup(SPWeb web, SPPrincipal principal, List<SPUser> users)
  {
            if (principal.GetType() == typeof(SPUser))
            {
                SPUser user = principal as SPUser;
                if (!user.IsDomainGroup)
                {
                    users.Add(user);
                }
                else
                {
                    SPPrincipalInfo[] groupMembers = SPUtility.GetPrincipalsInGroup(web, principal.LoginName, int.MaxValue - 1, out bool reachedMaxCount);


                    if (groupMembers == null) return;

                    foreach (SPPrincipalInfo member in groupMembers)
                    {
                        switch (member.PrincipalType)
                        {
                            case SPPrincipalType.SecurityGroup:
                                {
                                    ResolveGroup(web, web.EnsureUser(member.LoginName), users);
                                    break;
                                }
                            case SPPrincipalType.DistributionList:
                                {
                                    ResolveGroup(web, web.EnsureUser(member.LoginName), users);
                                    break;
                                }
                            case SPPrincipalType.SharePointGroup:
                                {
                                    ResolveGroup(web, web.Groups[member.LoginName], users);
                                    break;
                                }
                            case SPPrincipalType.User:
                                {
                                    try { users.Add(web.EnsureUser(member.LoginName)); }
                                    catch (Exception) { }//build proper error handling!
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                }
            }
            else
            {
                SPGroup group = principal as SPGroup;
                foreach (SPUser groupUser in group.Users)
                {
                    ResolveGroup(web, groupUser, users);
                }
            }
        }

SPPrincipalInfo[] groupMembers : Shows the users less than actually they are more thanks in advance, Mirza

  • The problem seems to be like you are getting only domain users and it is not returning Non-Domain users because of GetPrincipalsInGroup method returns only Domain users . – Technacron Oct 22 '18 at 11:56
  • what could be the solution? – Meerza Khizar Bayeg Oct 22 '18 at 12:01
  • @MeerzaKhizarBayeg Get all users (SP/AD) refers to get users based on SP or AD group. – Sohail Shaikh Oct 22 '18 at 12:23
  • @SohailShaikh below is the complete Scenario: I have a sharepoint sitecollection, which has a group assigned which is an AD distribution list. I resolve it into users but i get not all users – Meerza Khizar Bayeg Oct 22 '18 at 12:27
  • @SohailShaikh when I resolve it I get results in "SPPrincipalInfo[] groupMembers" the users i get are all from (germany DE\user1, DE\user2 and etc) what missing is the users from US (US\user1, US\user2) – Meerza Khizar Bayeg Oct 22 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.