locked
Client Object Model Get SiteGroups (By Name?) RRS feed

  • Question

  • I have the following code in a Client Object Model method:

    Group oGroup = clientContext.Web.SiteGroups.GetById(mGroupID);

    However, I need to get the group by name. In order to do this I have retrieved a collection of group names and IDs using the usergroup.asmx and searched through it to find the ID, finally applying the above code. It works, but is not cool enough.

    I have not found any other method on the SiteGroups collection which would allow me to search by name.  The .FirstOrDefault method throws an error saying that Lambdas are not allowed.

    I am looking for something maybe that would allow me to pass a CAML query such as the ListItemCollection which has the .GetItems method.

    Friday, June 17, 2011 5:12 PM

Answers

  • public static void GetSiteGroup()
    {
          ClientContext clientContext = new ClientContext("http://basesmc2008");
          Web web = clientContext.Web;
          GroupCollection groups = web.SiteGroups;
    
    
          clientContext.Load(groups, groupitems => groupitems.Include(groupitem => groupitem.Title,
            groupitem => groupitem.Description).Where(groupitem=> groupitem.Title == "Home Owners"));
    
          clientContext.ExecuteQuery();
    
    }
    


    The following code shows how you can use the include extension method along with a lambda expression to get the group by it's title. Any other properties you want to use should be added to the included list.

     


    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    • Marked as answer by Porter Wang Monday, June 20, 2011 2:01 AM
    Friday, June 17, 2011 8:03 PM

All replies

  • public static void GetSiteGroup()
    {
          ClientContext clientContext = new ClientContext("http://basesmc2008");
          Web web = clientContext.Web;
          GroupCollection groups = web.SiteGroups;
    
    
          clientContext.Load(groups, groupitems => groupitems.Include(groupitem => groupitem.Title,
            groupitem => groupitem.Description).Where(groupitem=> groupitem.Title == "Home Owners"));
    
          clientContext.ExecuteQuery();
    
    }
    


    The following code shows how you can use the include extension method along with a lambda expression to get the group by it's title. Any other properties you want to use should be added to the included list.

     


    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    • Marked as answer by Porter Wang Monday, June 20, 2011 2:01 AM
    Friday, June 17, 2011 8:03 PM
  • Can you please show this code in ecma javascript.
    Saturday, July 9, 2011 4:54 AM
  • You can do something like this in ECMA:

        var context = SP.ClientContext.get_current();

        var group = context.get_web().get_siteGroups().getByName("Home Owners");

        context.load(group);

        context.executeQueryAsync(function() {

          // success

        }, function(sender, args) {

          // failure

        });

    Monday, June 24, 2013 6:52 PM
  • object doesn't support property or method 'getByName'? are you certain this approach works? is there another way to query and get the groups id by its name? thanks for the help; appreciated~!

    Sunday, July 14, 2013 2:53 PM
  • Hi All,

    Even I use this code and got the error  "object doesn't support property or method 'getByName'".....please help me regarding this......

    var context = SP.ClientContext.get_current();

        var group = context.get_web().get_siteGroups().getByName("Home Owners");

        context.load(group);

        context.executeQueryAsync(function() {

          // success

        }, function(sender, args) {

          // failure

        });

    Friday, December 12, 2014 12:14 PM