安全地返回符合条件的网站集中的所有列表

时间:2012-10-15 05:05:41

标签: security list sharepoint sharepoint-2010

我正在使用以下代码获取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;
}

3 个答案:

答案 0 :(得分:1)

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

using Microsoft.Sharepoint.Administrator;

 SPSecurity.RunWithElevatedPrivileges(delegate(){

                // Your source code goes here
  });

答案 1 :(得分: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);

答案 2 :(得分: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;
    }