Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
    
What's wrong with just going to /_layouts/groups.aspx on your rootweb? It lists all groups existing on your site –  Daniel Ziga Feb 7 '12 at 12:56
1  
Programmatically =/ So i must use the SP Api! –  Ziba Leah Feb 7 '12 at 13:50

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
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 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 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 at 21:25

Have a look at this article.

Programmatically get SharePoint Users with Group Name

Hope this helps You!

share|improve this answer
    
We require relevant answers and disclosure of any affiliation with products or websites. Please see May I promote products or websites I am affiliated with here? for guidelines. –  SPDoctor May 9 '12 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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