CAML- WebService Sharepoint

时间:2013-05-13 09:07:21

标签: c# asp.net sharepoint caml

Hello stackoverflow社区

我从这个论坛得到了很多帮助。虽然这次我找不到。

我制作了一个ASP.NET应用程序,并尝试使用SharePoint WebService来获取列表中的某些项目。

到目前为止,我成功使用CAML请求获取整个列表,但我必须选择2个给定日期之间的项目

我找到了很多帮助,我正在使用这种方法来格式化ISO 8601日期字符串:

private string FormatDateForCAML(DateTime theDate)
{
    string result = theDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
    return result;
}

这是建立CAML请求:

System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
query.InnerXml = 
        "<Where>"+
            "<And>"+
              "<Geq>"+
                  "<FieldRef Name=\"startdate\" />"+
                  "<Value Type=\"DateTime\" IncludeTimeValue=\"True\">" + theStart + "</Value>" +
              "</Geq>"+
              "<Lt>" +
                  "<FieldRef Name=\"enddate\" />" +
                  "<Value Type=\"DateTime\" IncludeTimeValue=\"True\">" + theEnd+ "</Value>" +
              "</Lt>" +
            "</And>"+
        "</Where>";

此查询没有返回任何错误:

System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);

但是返回的列表是空的,尽管它不是

感谢您的帮助。

编辑:我终于成功了,问题来自错误的请求,这里是正确的版本

System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
query.InnerXml = 
        "<Where>"+
            "<And>"+
              "<Geq>"+
                  "<FieldRef Name=\"startdate\" />"+
                  "<Value Type=\"DateTime\" IncludeTimeValue=\"True\">" + theStart + "</Value>" +
              "</Geq>"+
              "<Lt>" +
                  "<FieldRef Name=\"startdate\" />" +
                  "<Value Type=\"DateTime\" IncludeTimeValue=\"True\">" + theEnd+ "</Value>" +
              "</Lt>" +
            "</And>"+
        "</Where>";

感谢Roqz我使用了CAML查看器,我可以解决问题:我只能比较开始日期!

谢谢你们的帮助:)

3 个答案:

答案 0 :(得分:2)

在我的CAML Builder工具中,类似于你的查询会返回它应该的结果。 查询看起来像:

<Query>
    <Where>
        <And>
          <Geq>
            <FieldRef Name="Created" />
            <Value IncludeTimeValue="TRUE" Type="DateTime">2013-04-01T19:35:49Z</Value>                 
          </Geq>
          <Lt>
            <FieldRef Name="Modified" /><Value IncludeTimeValue="TRUE" Type="DateTime">2013-05-24T19:36:46Z</Value>
          </Lt>
        </And>
     </Where>
</Query>

但是我记得,如果你想在代码中使用这个查询而不需要周围的标签。 你检查过你的日期格式是否正确?在你的例子中,我最后看不到“Z”。

答案 1 :(得分:0)

尝试使用完整的SharePoint对象模型在控制台应用程序中运行CAML查询,其中包含您想要在日期上过滤的任何子句。如果您的CAML在完整的SharePoint对象模型中工作,那么当您尝试使用SharePoint Native Web Services获取数据时,理想情况下也应该能够正常工作。

答案 2 :(得分:0)

我想补充一下这个方法:

private string FormatDateForCAML(DateTime theDate)
{
    string result = theDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
    return result;
}

相当于:

private string FormatDateForCAML(DateTime theDate)
{
    string result = theDate.ToString("s");
    return result;
}

date.ToString(“s”)返回ISO.8601日期格式字符串。