如何以编程方式确定SharePoint中的3个权限组访问者/成员/所有者?

Mag*_*son 4 permissions sharepoint

在SharePoint中,我们有3个预定的权限组:

  • 游客
  • 会员
  • 拥有者

作为/_layouts/permsetup.aspx页面中的设置.

(站点设置 - >人员和组 - >设置 - >设置组)

如何以编程方式获取这些组名?

(页面逻辑被Microsoft混淆,因此在Reflector中无法做到)

小智 10

SPWeb类上有属性:

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

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


Ale*_*gas 5

我发现各种"Associated ......"属性通常都是NULL.唯一可靠的方法是使用SPWeb上的属性包:

  • 访问者: vti_associatevisitorgroup
  • 成员: vti_associatemembergroup
  • 拥有者: vti_associateownergroup

要将它们转换为SPGroup对象,您可以使用:

int idOfGroup = Convert.ToInt32(web.Properties["vti_associatemembergroup"]);
SPGroup group = web.SiteGroups.GetByID(idOfGroup);
Run Code Online (Sandbox Code Playgroud)

但是,正如Kevin提到的那样,关联可能会丢失,这会在上面的代码中抛出异常.更好的方法是:

  1. 通过确保您要查找的属性确实存在,检查是否已在Web上设置了关联.

  2. 检查属性给出的ID实际存在的组.删除对SiteGroups.GetByID的调用,而是遍历SiteGroups中的每个SPGroup以查找ID.

更强大的解决方案:

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;
}
Run Code Online (Sandbox Code Playgroud)