How can i retrieve a list of users from a sharepoint group via CSOM where users displayname start with? RRS feed

  • Question

  • Hi,

    i have a problem with retrieving users from a sharepoint group via CSOM C#. I am currently retrieving all users via CSOM with my code below

        public List<SPUser> GetGroupMembers(string groupName)
            var appContext = _sharePointService.AppContext;
            Group group = appContext.Web.SiteGroups.GetByName(groupName);
            appContext.Load(group, grp => grp.Title, grp => grp.Users);
            List<ClientResult<PrincipalInfo>> principalsResults = new List<ClientResult<PrincipalInfo>>();
            foreach (var user in group.Users)
                principalsResults.Add(Utility.ResolvePrincipal(appContext, appContext.Web, user.LoginName, PrincipalType.User, PrincipalSource.All, null, false));
            List<SPUser> spUsers = new List<SPUser>();
            foreach (var principalResult in principalsResults)
            return spUsers.OrderBy(u => u.Name).ToList();

    But i only want users where displayname starts with "Mark*". Is this possible? How could i do that?

    best regards


    Best regards BOG

    Wednesday, March 15, 2017 8:28 AM

All replies

  • You can try something like below. It may help you.

    List<SPUser> spFilteredUsers = new List<SPUser>();
    spFilteredUsers = from SPUser u in spUsers
                           where u.Name.StartsWith("Mark")
                           select u;
    return spFilteredUsers.OrderBy(u => u.Name).ToList();

    Wednesday, March 15, 2017 10:05 AM
  • Hi Jaydeep,

    wouldnt this mean that i have to load first of all ALL users from the group? What i want to have is to query on the users which starts with "XYZ" from the group to avoid querying first all users then do a filtering in memory of the response. This is veeeeery slow.

    Isnt there a way to extend this function with Beginswith or startswith or something like that?

    appContext.Load(group, grp => grp.Title, grp => grp.Users);


    appContext.Load(group, grp => grp.Title, grp => grp.Users.StartsWith("ABC"));

    Best regards BOG

    • Edited by BOG's Lab Wednesday, March 15, 2017 10:31 AM
    Wednesday, March 15, 2017 10:30 AM
  • I don't think it is possible with CSOM using expression like contain or StartsWith

    You can use the REST API which gives you more flexibility.

    the following will return any users from the "Approvers" group containing name Reshmee


    the following will return users beginning with Reshmee


    • Proposed as answer by reshmee011 Wednesday, March 15, 2017 10:47 PM
    • Unproposed as answer by reshmee011 Wednesday, March 15, 2017 10:47 PM
    • Proposed as answer by reshmee011 Wednesday, March 15, 2017 10:48 PM
    Wednesday, March 15, 2017 12:04 PM