1

我正在使用以下代码来获取 Web 应用程序网站集中的所有“公告”列表。

不幸的是,有时当前用户没有访问该站点的权限,并且页面会因异常而失败,即使在 try 块内也是如此。

为所有用户安全地执行以下操作的正确方法是什么,即使是匿名用户也不会得到任何结果?

static public List<SPListMeta> AllSiteAnnouncementsLists()
{
    var returnList = new List<SPListMeta>();
    foreach (SPSite oSiteCollection in SPContext.Current.Web.Site.WebApplication.Sites)
    {
        var collWebs = oSiteCollection.AllWebs;
        try
        {
            foreach (SPWeb oWebsite in collWebs)
            {
                using (oWebsite)
                {
                    var collSiteLists = oWebsite.GetListsOfType(SPBaseType.GenericList);
                    returnList.AddRange(from SPList oList in collSiteLists where oList.Title == "Announcements" select new SPListMeta(oList));
                }
            }
        }
        catch
        {
        }

    }
    return returnList;
}
4

3 回答 3

1

尝试为您的代码提供正确的执行权限。

using Microsoft.Sharepoint.Administrator;

 SPSecurity.RunWithElevatedPrivileges(delegate(){

                // Your source code goes here
  });
于 2012-10-15T06:26:59.983 回答
1

要从网站集中获取特定列表类型的所有项目,您必须使用 SPSiteDataQuery。每个用户将只获得他们拥有权限的那些项目。

     SPWeb web = SPContext.Current.Web;
     SPSiteDataQuery query = new SPSiteDataQuery();

     //Ask for all lists created from the announcement template.
     query.Lists = "<Lists ServerTemplate=\"104\" />";

     // Get the Title field. Define here all you need.
     query.ViewFields = "<FieldRef Name=\"Title\" />";

     // Set the sort order.
     query.Query = "<OrderBy>" + 
                       "<FieldRef Name=\"Title\" />" + 
                   "</OrderBy>";

     // Query all Web sites in this site collection.
     query.Webs = "<Webs Scope=\"SiteCollection\" />";

     DataTable dt = web.GetSiteData(query);
     DataView dv = new DataView(dt);
于 2012-10-15T09:31:37.550 回答
0

这就是最终为我工作的方法,尽管我不知道这是否是最好的方法。

    static public List<SPListMeta> AllSiteAnnouncementsLists()
    {
        var returnList = new List<SPListMeta>();

        var collWebs = SPContext.Current.Web.Site.WebApplication.Sites[0].OpenWeb().GetSubwebsForCurrentUser();
        if(SPContext.Current.Site.RootWeb.DoesUserHavePermissions(SPBasePermissions.Open))
        {
            var collSiteLists = SPContext.Current.Site.RootWeb.GetListsOfType(SPBaseType.GenericList);
            returnList.AddRange(from SPList oList in collSiteLists
                                where oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems)
                                && oList.BaseTemplate == SPListTemplateType.Announcements
                                select new SPListMeta(oList));
        }
        foreach (SPWeb oWebsite in collWebs)
        {
            returnList.AddRange(WebRecursion.GetListsForCurrentWeb(oWebsite, SPListTemplateType.Announcements));
            foreach (SPWeb oSubSite in oWebsite.Webs)
            {
                returnList.AddRange(WebRecursion.GetListsForCurrentWeb(oSubSite, SPListTemplateType.Announcements));
            }
        }
        return returnList;
    }

    public static List<SPListMeta> GetListsForCurrentWeb(SPWeb oWebsite, SPListTemplateType type)
    {
        var returnList = new List<SPListMeta>();
        if (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))
        {
            using (oWebsite)
            {
                var collSiteLists = oWebsite.Lists;
                returnList.AddRange(from SPList oList in collSiteLists
                                    where oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems)
                                    && oList.BaseTemplate == type
                                    select new SPListMeta(oList));
            }
        }
        return returnList;
    }
于 2013-01-01T00:05:24.423 回答