4

I need to know all groups in my site collection... If I use

  using (SPSite scSite = new SPSite(fullsite))
                {
                    using (SPWeb scWeb = scSite.OpenWeb())
                    {
                        foreach(SPGroup spg in scWeb.Groups)
                        {
                            debug += spg.Name + "!!!";
                        }
                    }
                }

The only group I get are groups which have Permissions on the site... How can I get also the groups that has no permission on the site but exists (and has permission in subsites)? There is an easy way or I have to loop on childs and get new groups from there??

Thank you very much!

2
  • What's wrong with just going to /_layouts/groups.aspx on your rootweb? It lists all groups existing on your site
    – user2536
    Feb 7, 2012 at 12:56
  • 1
    Programmatically =/ So i must use the SP Api!
    – Ziba Leah
    Feb 7, 2012 at 13:50

2 Answers 2

9

You need SPWeb.SiteGroups (MSDN link) which includes all groups that exist in the site collection.

For further details, please refer to: http://blog.tylerholmes.com/2007/09/spwebgroups-vs-spwebsitegroups.html

3
  • References are very nice, but simply saying foreach (SPGroup group in scWeb.SiteGroups) might have been nicer than casting someone off to a link right away, though.
    – vapcguy
    Apr 29, 2015 at 1:25
  • @vapcguy, the foreach statement is already exists in the question body. So I don't see the need to copy it into the answer because the question was not about the way to deal with the collection of the groups - only about which collection is right for the case. The only thing that needs to be changed in the code from the question is the SPWeb's property containing the required groups. And that's what my answer is about exactly. IMHO, the typical useful answer is not required to be so simple as copy-paste. There's no need to follow the links, the answer is right at the first 4 words of the text.
    – Alex Boev
    Apr 29, 2015 at 8:26
  • Except when I read your answer, I didn't realize how you were applying SPWeb.SiteGroups, especially since .Groups was already there, until I looked at the links and figured out the difference from that. Simplicity for those that might stumble upon this, especially to those new to this concept as I was, was all I was looking for.
    – vapcguy
    Apr 29, 2015 at 21:25
2

Have a look at this article.

Programmatically get SharePoint Users with Group Name

Hope this helps You!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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