0

I am trying to convert my rest implementation to CSOM.

Rest API for the same : _api/web/sitegroups?$Filter=LoginName%20eq%20'Test_Grp'

Unable to find an equivalent for $filter in lambda expressions.

I have a list of groupNames and I need to fetch only those groups from SharePoint.

The below snippet throws a ClientRequestException that

'The 'Contains' member cannot be used in the expression.

How can I retrieve only the groups whose names are in my list?

  using (var clientContext = new ClientContext(ServerUrl))
  {
       GroupCollection collGroup = clientContext.Web.SiteGroups;

            clientContext.Load(collGroup);

            clientContext.Load(collGroup,
          groups => groups.Include(
              group => group.LoginName,
             group => group.Title,
          group => group.Id), group => group.Where(i => names.Contains((string)i.LoginName)));
            clientContext.ExecuteQuery();
  }
1

REST request: /_api/web/sitegroups?$filter=LoginName eq '<group name>' could be converted to the following CSOM equivalent:

using (var context =  new ClientContext(webUri))
{         
      var result = context.LoadQuery(context.Web.SiteGroups.Where(g => g.LoginName == groupName));
      context.ExecuteQuery();
      var foundGroup = result.FirstOrDefault(); //get first result. Use .ToList() to retrieve all results  

      if (foundGroup != null) 
           Console.WriteLine(foundGroup.Title);

}
2
  • Thanks for the response Vadim. I have a small issue here. It works to get a single group but what i want is to fetch a list of groups given their names. I need to use var result = clientContext.LoadQuery(clientContext.Web.SiteGroups.Where(g => names.Contains(g.LoginName))); I get an exception - The 'Contains' member cannot be used in the expression.
    – jkr
    Oct 5 '15 at 2:28
  • names is actually a list of strings
    – jkr
    Oct 5 '15 at 2:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.