October 1, 2007

MOSS: SPWeb.SiteGroups vs. SPWeb.Groups

When creating a site and adding groups to it programmatically, I add groups to the new site's SiteGroups collection like this:

newSite.SiteGroups.Add(grpOwnersName, currentUser, currentUser, grpOwnersName);

Then I'd like to define some properties of the new group, and I try to do this:

grpOwners = newSite.Groups[grpOwnersName];

Oh no, the Groups collection is empty! How and when is it populated?

Of course I can use SiteGroups, but I'd very much like to use Groups collection since there are zillions of groups in the SiteGroups collection, and I'm a performance freak.

grpOwners = newSite.SiteGroups[grpOwnersName];



  1. I have same question! Please, answer-me if you know now.

  2. Solution is still unknown, in http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.groups%28v=office.12%29.aspx comments it is mentioned that "SPWeb.Groups gives all the site groups that have been given access either to the entire SPWeb or to any single object in SPWeb like list or listitem", but this doesn't seem to be the case.

    Verify that you have latest Service Pack and cumulative update for MOSS 2007, maybe this has been fixed in some CU.

  3. SPWeb.Groups returns collection of cross-site groups which has some permission on the site. So if you add group from a site without any permission on the site, then this group wont appear in SPWeb.Groups collection, but it will appear in SPWeb.SiteGroups collection.
    See this article: http://sridharu.blogspot.com/2009/04/difference-between-spwebgroups-vs.html
    I suggest you try using groups and if this returns null, then check SiteGroups.