如何编写 CAML 查询以使结果包含 Column1 和 Column2?

发布于 2025-01-02 22:02:40 字数 403 浏览 0 评论 0原文

我的下面的代码用于根据 ID 进行过滤:

camlQuery = xmlDoc.CreateElement("Query");

                camlQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";

如何编写 CAML 查询以使其返回(结果包含)Column1 和 Column2?

我是 CAML 查询的新手,也许无法过滤并说只应返回某些列?

问题是,当我运行上述查询时,它返回列表中的所有 SharePoint 列 (42),而我只需要 2-3 个。

My below code is used for filtering based on ID:

camlQuery = xmlDoc.CreateElement("Query");

                camlQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";

How to write a CAML Query so that it returns (the result contains) Column1 and Column2?

I'm new to CAML Queries, maybe it's not possible to filter and say only certain columns should be returned?

The problem is that when I run the above query, it returns all the SharePoint columns of my list (42), whereas I only need 2-3.

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

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

发布评论

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

评论(3

只怪假的太真实 2025-01-09 22:02:40

您必须使用 SPQuery.ViewFields 属性。

You have to use the SPQuery.ViewFields property.

一曲琵琶半遮面シ 2025-01-09 22:02:40

SPQuery.Query 的 MSDN 文档 提供了一个很好的示例,说明如何使用 SPQuery 对象的 Query 属性和 ViewFields 属性:

SPList oList = oWebsiteRoot.Lists["List_Name"];
SPQuery oQuery = new SPQuery();
oQuery.ViewFields = "<FieldRef Name='Column1'/><FieldRef Name='Column2'/>";
oQuery.Query = "<Where><Gt><FieldRef Name='ID'/>" + 
               "<Value Type='Number'>0</Value></Gt></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

MSDN documentation of SPQuery.Query provides a nice example how to use both Query property and the ViewFields property of the SPQuery object:

SPList oList = oWebsiteRoot.Lists["List_Name"];
SPQuery oQuery = new SPQuery();
oQuery.ViewFields = "<FieldRef Name='Column1'/><FieldRef Name='Column2'/>";
oQuery.Query = "<Where><Gt><FieldRef Name='ID'/>" + 
               "<Value Type='Number'>0</Value></Gt></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);
一张白纸 2025-01-09 22:02:40

尝试使用下面的代码:

    XmlDocument xmlDoc = new System.Xml.XmlDocument();
    XmlElement query = xmlDoc.CreateElement("Query");
    XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
    XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

    query.InnerText = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";
    viewFields.InnerXml = "<FieldRef Name=\"Column1\" /><FieldRef Name=\"Column2\" />";
    queryOptions.InnerXml = "";

    try
    {
        XmlNode ndListItems = listService.GetListItems("List_Name", null, query, viewFields, null, queryOptions, null);
    }

参考: msdn 示例

Try with the code below:

    XmlDocument xmlDoc = new System.Xml.XmlDocument();
    XmlElement query = xmlDoc.CreateElement("Query");
    XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
    XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

    query.InnerText = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";
    viewFields.InnerXml = "<FieldRef Name=\"Column1\" /><FieldRef Name=\"Column2\" />";
    queryOptions.InnerXml = "";

    try
    {
        XmlNode ndListItems = listService.GetListItems("List_Name", null, query, viewFields, null, queryOptions, null);
    }

For references: msdn example

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