需要将单个文档库上的 SPQuery 转换为站点中所有文档库上的 SPSiteDataQuery

发布于 2024-07-28 22:14:44 字数 1467 浏览 5 评论 0 原文

我需要帮助将以下代码片段转换为使用 SPSiteDataQuery 而不是 SPQuery b/c 我需要查询站点中的所有文档库。

这是原始代码:

using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
                {
                    SPList oList = oWeb.Lists["Quality Documents"];

                    //create query
                    SPQuery oQuery = new SPQuery();

                    //configure the query  //
                    oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";

                    //get the collection of items in the list
                    SPListItemCollection oItems = oList.GetItems(oQuery);

                    if (oItems.Count > 0)
                    {
                        newRnd = 0;
                    }
                    else
                    {
                        newRnd = 1;
                    }
                }

这是我的 SPSiteDataQuery 代码,但我不知道它是否正确和/或如何确保代码与上面的代码执行相同的操作。

SPSiteDataQuery q = new SPSiteDataQuery();
q.Lists = "<Lists BaseType='1'/>";
q.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.ViewFields = "<FieldRef Name='Document_x0020_Number' />"';
q.RowLimit = 1;

I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery b/c I need to query accross all Document Libraries in the site.

Here's the original code:

using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
                {
                    SPList oList = oWeb.Lists["Quality Documents"];

                    //create query
                    SPQuery oQuery = new SPQuery();

                    //configure the query  //
                    oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";

                    //get the collection of items in the list
                    SPListItemCollection oItems = oList.GetItems(oQuery);

                    if (oItems.Count > 0)
                    {
                        newRnd = 0;
                    }
                    else
                    {
                        newRnd = 1;
                    }
                }

And here's what I have for the SPSiteDataQuery but I don't know if it's correct and/or how to make sure the code does the same thing as the above code.

SPSiteDataQuery q = new SPSiteDataQuery();
q.Lists = "<Lists BaseType='1'/>";
q.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.ViewFields = "<FieldRef Name='Document_x0020_Number' />"';
q.RowLimit = 1;

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

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

发布评论

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

评论(1

爱人如己 2024-08-04 22:14:44

SPQuerySPSiteDataQuery用于指定要查询的列表。 与 SPQuery 不同,在 SPSiteDataQuery 中,您无法指定列表标题来缩小每个站点中的查询范围,您只有 Lists 元素,灵活性较差。 Vincent Rothwell 描述了所有可能性在这里

这种不灵活性意味着在列表元素代码中使用 BaseType=1 将返回所有文档库中的文档。 避免这种情况的唯一方法是为质量文档部署一个具有自己的 ID 号的自定义列表模板。 然后,您就可以使用 (其中 XYZ 是 ID 号,用于将结果范围缩小到该类型的列表)。 否则,您将需要过滤返回的结果,以便它们仅包含质量文档库中的条目。

除此之外,您的代码看起来是等效的。 您可能还想查看 CrossListQueryInfoCrossListQueryCache(如果您运行的是 MOSS)。 这些允许您缓存 SPSiteDataQuery 样式的查询。 有关详细信息,请参阅 Jeff Dalton 的这篇文章这。

更新有关对象处置的评论:

您的代码将泄漏 SPSite 对象 - 这也需要一个 using 子句。 将当前的 using 行替换为以下行:

using (SPSite oSite = new SPSite(properties.SiteId))
using (SPWeb oWeb = oSite.OpenWeb(properties.RelativeWebUrl))
{
    // Your code
}

此外,使用 SPDisposeCheck 也很容易。 要与 Visual Studio 集成,请将以下行添加到项目的构建后事件中:

“%ProgramFiles%\Microsoft\SharePoint
处置检查\SPDisposeCheck.exe”
$(目标路径)

如果存在内存泄漏,构建现在将失败。 检查“输出”窗口以获取有关它们所在位置的详细信息。 请注意,SPDisposeCheck 有时会报告误报,并且可以忽略这些(阅读文档以了解如何操作)。

The main difference between SPQuery and SPSiteDataQuery is in specifying the list to query. Unlike with SPQuery, in SPSiteDataQuery you cannot specify a list title to narrow down your query in each site, you only have the Lists element which is a lot less flexible. Vincent Rothwell describes all of the possibilities here.

This inflexibility means that the use of BaseType=1 in your Lists element code will return documents from all document libraries. The only way you can avoid this is if you deployed a custom list template for Quality Documents with its own ID number. You would then be able to use <Lists ServerTemplate='XYZ' /> (where XYZ is the ID number to narrow results down to that type of list). Otherwise you will need to filter the results you get back so they only contain entries from the Quality Documents library.

Apart from this your code looks equivalent. You may also like to look at CrossListQueryInfo and CrossListQueryCache if you are running MOSS. These allow you to cache SPSiteDataQuery-style queries. See this post by Jeff Dalton for good information on this.

Update from comments about object disposal:

Your code will leak the SPSite object - this needs a using clause as well. Replace the using line you currently have with these lines:

using (SPSite oSite = new SPSite(properties.SiteId))
using (SPWeb oWeb = oSite.OpenWeb(properties.RelativeWebUrl))
{
    // Your code
}

Also, it is easy to use SPDisposeCheck. To integrate with Visual Studio add the following line to your project's post build event:

"%ProgramFiles%\Microsoft\SharePoint
Dispose Check\SPDisposeCheck.exe"
$(TargetPath)

The build will now fail if you have memory leaks. Check the Output window for details on where they are. Note that SPDisposeCheck sometimes reports false positives and it is possible to ignore these (read the documentation to tell you how).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文