查询整个网站集共享点

发布于 2024-08-18 15:17:04 字数 1008 浏览 3 评论 0原文

我有一个 Web 部件,位于顶级站点下的 2-3 个子站点。我需要查询顶级网站集中的列表和同一级别的列表,我想它可以通过 SPSiteDataquery...我对此有一些困惑,我可以编写可以查询这两个列表的单个查询吗...

此查询的范围是 sitecollection,因此这意味着它将查看 sitecollection 中的所有列表。如果我的 CAML 查询对于这两个列表都相同...它应该可以工作吗?

让我通过我的代码进行解释:

SPSite mySite = SPControl.GetContextSite(Context);
                SPWeb myWeb = SPControl.GetContextWeb(Context);
                SPSiteDataQuery qry = new SPSiteDataQuery();
                qry.Lists = "<Lists BaseType='0' />";

                qry.Query = "<Where><Contains><FieldRef Name='Country'/><Value Type='Text'>" + strcount + "</Value></Contains></Where>";

                qry.ViewFields = "<FieldRef Name='Capital' Nullable='TRUE'/><FieldRef Name='Currency' Nullable='TRUE'/>";

                qry.Webs = "<Webs Scope='SiteCollection' />";

                DataTable dt = myWeb.GetSiteData(qry);

现在我需要顶级站点列表中的货币和同一级别列表中的资本。这可能吗?或者我误解了 SPSiteDataQuery...?

I have a webpart which is 2-3 subsites down the top level site. I need to query the list which is in top site collection and one at the same level,I guess its possible through SPSiteDataquery...I have some confusion related to it can i write single query which can query both these list....

The scope of this query is sitecollection so that means it wud going to look into all list in sitecollection..and if my CAML query is same for both these lists ...it should work?

let me explain through my code:

SPSite mySite = SPControl.GetContextSite(Context);
                SPWeb myWeb = SPControl.GetContextWeb(Context);
                SPSiteDataQuery qry = new SPSiteDataQuery();
                qry.Lists = "<Lists BaseType='0' />";

                qry.Query = "<Where><Contains><FieldRef Name='Country'/><Value Type='Text'>" + strcount + "</Value></Contains></Where>";

                qry.ViewFields = "<FieldRef Name='Capital' Nullable='TRUE'/><FieldRef Name='Currency' Nullable='TRUE'/>";

                qry.Webs = "<Webs Scope='SiteCollection' />";

                DataTable dt = myWeb.GetSiteData(qry);

Now i need currency from list which is in top level site and Capital from the list which is at same level. Is this possible? or I misunderstood SPSiteDataQuery...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-08-25 15:17:04

您走在正确的道路上,可以检索不同网站中列表的结果。以下示例演示如何从默认“任务”或“工作流程任务”列表中检索项目,并使用在根级别和子站点内创建的任务列表。

        SPSiteDataQuery q = new SPSiteDataQuery();
        q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
        q.Webs = "<Webs Scope='SiteCollection' />";
        q.Lists = "<Lists BaseType='0' />";
        q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";

        DataTable results = new DataTable();

        using (SPSite site = new SPSite("http://sharepoint"))
        {
            using (SPWeb web = site.OpenWeb("subsite"))
            {                    
                results = web.GetSiteData(q);
            }
        }

我使用硬编码 URL 编写了它,以便您可以在控制台应用程序中运行它进行测试,但是当您放置此内容时,您可以将 using 语句替换为 SPWeb web = SPContext.Current.Web;在 Web 部件内。

其他一些值得考虑的事情:

  • 您正在查询的列表必须包含 ViewFields 元素中的所有字段
  • 多重查找字段不能与 SPSiteDataQuery 很好地配合(单值查找字段也可以)
  • u2u CAML构建器工具对于测试 CAML 查询也很有用。请参阅http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx

You are on the right track and it is possible to retrieve results for lists in different webs. The following example shows how to retrieve items from the default 'Tasks' or 'Workflow Tasks' lists and works with task lists created at the root level and within a sub site.

        SPSiteDataQuery q = new SPSiteDataQuery();
        q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
        q.Webs = "<Webs Scope='SiteCollection' />";
        q.Lists = "<Lists BaseType='0' />";
        q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";

        DataTable results = new DataTable();

        using (SPSite site = new SPSite("http://sharepoint"))
        {
            using (SPWeb web = site.OpenWeb("subsite"))
            {                    
                results = web.GetSiteData(q);
            }
        }

I've written it using a hardcoded URL so you can run it inside a console application for testing but you can replace the using statements with something like SPWeb web = SPContext.Current.Web; when you put this inside a web part.

A few other things worth considering:

  • The lists you are querying must contain all the fields in the ViewFields element
  • Multi-lookup fields do not work well with the SPSiteDataQuery (single value lookup fields are ok)
  • The u2u CAML builder tool is also useful for testing CAML queries. See http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文