无法使用SPSecurity.RunWithElevatedPrivileges访问站点用户和组 [英] Unable to access Site Users and Groups using SPSecurity.RunWithElevatedPrivileges
问题描述
亲爱的所有人,
我无法使用以下代码访问共享点用户和组,以访问Web应用程序的所有用户和网站集的组,包括其子站点和子子站点
任何人都可以让我知道下面的代码有什么问题以及如何解决吗
Dear All,
I am unable to access the sharepoint users and groups using below code to access all the users and groups of site collections of web application including their sub-sites and child sub-sites
Can any one do let me know what's wrong in my below code and how can I solve it
Guid siteId = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(delegate(){
try
{
using (SPSite _site = new SPSite(siteId))
{
SPSiteCollection tmpRootColl = _site.WebApplication.Sites;
foreach (SPSite tmpSite in tmpRootColl)
{
// using (SPWeb elevatedWeb = _site.OpenWeb(CurrentWeb.ID))
//foreach (SPWeb elevatedWeb in _site.AllWebs)
foreach (SPWeb elevatedWeb in tmpSite.AllWebs)
{
}
}
}
}
推荐答案
不确定您要完成的任务是什么还是什么,但这是我用来遍历各个组的目的我的应用程序.如果您想对小组做更具体的事情,然后遍历他们,我也许可以提供更多 特定的帮助,但是我基本上使用它来遍历各个组并确定用户属于哪个组:
Not sure what exactly your trying to accomplish or anything but this is what I am using to loop through the groups in my application. If you are trying to do something more specific with the groups then loop through them I may be able to offer some more specific help but I basically use this to loop through the groups and determine which group(s) a user belongs to:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication oApp = SPContext.Current.Site.WebApplication;
foreach (SPSite oSite in oApp.Sites)
{
foreach (SPWeb oWeb in oSite.AllWebs)
{
foreach (SPGroup group in oWeb.Groups)
{
try
{
if (group.ContainsCurrentUser)
{
htUserGroups.Add(group.Name.ToString(), group.Name.ToString());
}
}
catch
{
//SharePoint throws an exception if the group indexer
//is not a group the current user belongs to so no
//need to do anything but ignore the error .
}
}
}
}
});
这篇关于无法使用SPSecurity.RunWithElevatedPrivileges访问站点用户和组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!