从SharePoint 2010中的其他网站集检索列表项

时间:2011-02-17 20:13:38

标签: list sharepoint-2010 web-parts

我在从其他网站集中检索列表项时遇到问题。尝试从当前网站集中接收列表项时,我没有问题。例如,http://myintranet.com/Departments/IT有效。但是http://myintranet.com/sites/Departments/IT会返回错误。

if (!String.IsNullOrEmpty(SiteName) && !String.IsNullOrEmpty(SPContext.Current.Web.Url))
    {
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
        using (SPSite intranetSite = new SPSite(SPContext.Current.Web.Url))
        {
          using (SPWeb currentWeb = intranetSite.AllWebs["/sites/projects/Physics"])
          {
            SPList postList = currentWeb.Lists.TryGetList("Issues");                

            if (postList != null)
            {
              IssueList.DataSource = postList.Items.GetDataTable();

              IssueList.DataBind();
            }
          }
        } 

      });
    }

在尝试接收列表项时,我没有使用任何不同的代码。唯一的区别是这次我从另一个网站集中获取列表项。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

问题是intranetSite.AllWebs。这只会获取当前网站集下的SPWeb对象。

您无法直接从一个网站集推断出其他网站集。

即使/ sites / projects看起来像来自/的chid网站集,它也不是。 / sites只是一个托管路径。 /和/ sites / projects处于网站集层次结构的同一级别。

您需要做的是:

if (!String.IsNullOrEmpty(SiteName) && !String.IsNullOrEmpty(SPContext.Current.Web.Url))
    {
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {

          using (SPWeb currentWeb = new SPSite("http://server/sites/projects/Physics").OpenWeb())
          {
            SPList postList = currentWeb.Lists.TryGetList("Issues");                

            if (postList != null)
            {
              IssueList.DataSource = postList.Items.GetDataTable();

              IssueList.DataBind();
            }
          }

      });
    }
相关问题