【问题标题】:Retrieve List Items from a different Site Collection in SharePoint 2010从 SharePoint 2010 中的不同网站集中检索列表项
【发布时间】:2011-06-29 09:01:13
【问题描述】:

我在从其他网站集中检索列表项时遇到问题。尝试从当前网站集中接收列表项时,我没有遇到问题。例如,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();
            }
          }
        } 

      });
    }

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

感谢您的帮助!

【问题讨论】:

    标签: list sharepoint-2010 web-parts


    【解决方案1】:

    问题是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();
                }
              }
    
          });
        }
    

    【讨论】:

    • 现在我终于开始了解这个托管路径业务了。整天给我带来问题。谢谢你的帮助。明天我会尝试实现这个。
    • 不用担心 - 是的,他们一开始会感到困惑,因为他们搞砸了看起来像正常层次结构的东西。托管路径只是对网站集进行分组的一种“逻辑”方式,但根据对象模型,所有网站集在 Web 应用程序眼中都是平等的,无论它位于哪个托管路径下。
    猜你喜欢
    • 2011-03-16
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多