SharePoint C#获取用户有权限的站点和子站点列表(如读取,贡献访问者)

时间:2013-07-31 11:12:23

标签: sharepoint-2010 permissions sites contribute

using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            foreach (SPWeb oWeb in oSite.RootWeb.GetSubwebsForCurrentUser())
            {
                permission = string.Empty;
                foreach (SPGroup group in oWeb.Groups)
                {
                    foreach (SPUser u in group.Users)
                    {
                        if (u.Name == (userName))
                        {
                            foreach (SPRole role in u.Roles)
                            {
                                permission += role.Name.ToString() + ", ";
                            }
                                                       }
                    } // Taking All Permission details of user
                }
                permission = " [" + permission.TrimEnd(", ".ToCharArray()) + "]";

} }

我的最终字符串变量将包含[Read,Visitor]或[Read]或[visitor,Read]

等值

我想拥有所有的许可名称,实际上我在u.Roles上收到错误/警告[角色现在是绝对的],

通过使用SPRoleDefination等帮助我获得许可 SPRoleDefinitionCollectionroleDefinitions = oWeb.RoleDefinitions; SPRoleDefinitionroleDefinition

由于 普利文

1 个答案:

答案 0 :(得分:0)

试试这个

 foreach (SPWeb oWeb in oSite.RootWeb.GetSubwebsForCurrentUser())
            {

                SPRoleDefinitionCollection oCollection= oWeb.RoleDefinitions;

                foreach (SPRoleDefinition r in oCollection)
                {
                    SPRoleDefinition roleDef = oCollection[r.Name.ToString()];

                    foreach (SPRoleAssignment oAssigment in oWeb.RoleAssignments)
                    {
                        if (oAssigment.RoleDefinitionBindings.Contains(roleDef))
                        {
                            permissions+= r.Name.ToString();
                        }
                    }

                }