views:

796

answers:

4

In SharePoint, we have the 3 predetermined permission groups:

  • Visitors
  • Members
  • Owners

As setup in the /_layouts/permsetup.aspx page.

(Site settings->People and Groups->Settings->Setup groups)

How can a get these group names programmatically?

(The page logic is obfuscated by Microsoft, so no can do in Reflector)

+1  A: 

There are properties on the SPWeb class:

  • SPWeb.AssociatedVisitorGroup
  • SPWeb.AssociatedMemberGroup
  • SPWeb.AssociatedOwnerGroup

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.associatedmembergroup.aspx

DJ
Thanks, I had totally missed those.
Magnus Johansson
+1  A: 

Hey there, I'm Kevin and I'm the PM for SharePoint permissions at Microsoft.

DJ's answer is completely correct, but I'd warn that depending on what you're doing, this might not be the most robust thing to use. Users could blow away those groups and these associations would be lost. I'd definitely look to build some backup logic into whatever you're fetching these for.

Kevin Davis
Hi Kevin. We have multiple sub sites that have custom groups for these 3 on every sub site, and we have set them up in the mentioned permission page. This is more or less the only way we have for managing diversified permission (groups) for every sub site. I'm not doing any customization in regards of modifying the actual setup. The goal is to get a list of all users in each group to build up mailing lists. The risk of people doing harm ans stupid things with their permissioned rights will always be there in SharePoint.
Magnus Johansson
A: 

I tried this, but the AssociatedOwnerGroup/AssociatedMemberGroup/AssociatedVisitorGroup properties all return NULL even though the groups exist and SPWeb.AssociatedGroups returns all three. Why is that?

Frode
A: 

I've found the various "Associated..." properties to often be NULL. The only reliable way is to use the property bag on the SPWeb:

  • Visitors: vti_associatevisitorgroup
  • Members: vti_associatemembergroup
  • Owners: vti_associateownergroup

To convert them to an SPGroup object, you could use:

int idOfGroup = Convert.ToInt32(web.Properties["vti_associatemembergroup"]);
SPGroup group = web.SiteGroups.GetByID(idOfGroup);

However as Kevin mentions, the associations may be lost which would throw exceptions in the above code. A better approach is to:

  1. Check that associations have been set on the web by ensuring the property you are looking for actually exists.

  2. Check that the group with ID given by the property actually exists. Remove the call to SiteGroups.GetByID and instead loop through each SPGroup in SiteGroups looking for the ID.

The more robust solution:

public static SPGroup GetMembersGroup(SPWeb web)
{
    if (web.Properties["vti_associatemembergroup"] != null)
    {
        string idOfMemberGroup = web.Properties["vti_associatemembergroup"];
        int memberGroupId = Convert.ToInt32(idOfMemberGroup);

        foreach (SPGroup group in web.SiteGroups)
        {
            if (group.ID == memberGroupId)
            {
                return group;
            }
        }
    }
    return null;
}
Alex Angas

related questions