无法通过在REST中传递CAML查询来获取总项数 [英] Unable to get Count of Total Item by passing CAML Query in REST

查看:31
本文介绍了无法通过在REST中传递CAML查询来获取总项数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我试图通过REST下面的CAML查询来查找总项数。

但是我找不到运气。

I am trying to find count of total items by pass the CAML Query in REST with my below.
But I find no luck in it.

任何人都可以让我知道它有什么问题

Can any one please do let me know what's wrong in it

function Get_Category_Total()
 {
    $.ajax({
    
    url: _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/GetByTitle('OrderDetails')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query><Where><And><And><Or><Or><Or><Or><Eq><FieldRef Name='Country' /><Value Type='Text'>USA</Value></Eq><Eq><FieldRef Name='Country' /><Value Type='Text'>UK</Value></Eq></Or><Eq><FieldRef Name='Country' /><Value Type='Text'>UAE</Value></Eq></Or><Eq><FieldRef Name='Country' /><Value Type='Text'>Germany</Value></Eq></Or><Eq><FieldRef Name='Country' /><Value Type='Text'>France</Value></Eq></Or><Eq><FieldRef Name='Category' /><Value Type='Text'>Grains</Value></Eq></And><Eq><FieldRef Name='Code' /><Value Type='Calculated'>XLR</Value></Eq></And></Where></Query></View>"}&$select=Title,ID"
    

               type: "POST",
               headers: {"accept": "application/json;odata=verbose"},
               success: function (data) {
                  if (data.d.results) {
                        var itemsCount = data.d.results.length;
                      alert(itemsCount);
                   }
             },
             error: function (xhr)
             {
                alert(xhr.status + ': ' + xhr.statusText);
             }
       });
 }

SP Assest

SP Assest

推荐答案

请像这样使用CAML Query和Rest API,将CAML视为元数据而不是直接在GetItems中使用:

Please use CAML Query with Rest API like this, regard CAML as a metadata rather than use in GetItems directly:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var caml = "<View><Query><Where><Eq><FieldRef Name='EndDate'/><Value Type='DateTime'><Today /></Value></Eq></Where></View></Query>"; 
restCallwithCaml("RestTest", caml);  
function restCallwithCaml(listName, caml) {  
    /// get the site url  
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;  
    /// set request data  
    var metadata = { "query" :{"__metadata": { "type": "SP.CamlQuery" }, "ViewXml": caml}};  
    /// make an ajax call


.ajax({
url:siteUrl +" / _ api / web / lists / GetByTitle('" + listName +"')/ GetItems",
方法:" POST",
数据:JSON.stringify(元数据),
标题:{
" X-RequestDigest" :
.ajax({ url: siteUrl+"/_api/web/lists/GetByTitle('"+ listName +"')/GetItems", method: "POST", data: JSON.stringify(metadata), headers: { "X-RequestDigest":


(" #__ REQUESTDIGEST")。val(),
'content-type':'application / json; odata = verbose',
'accept ':'application / json; odata = verbose'
},
success:function(data){
console.log(data);
console.log(data.d .results.length);
},
错误:函数(数据){
///做你的代码
}
});
}
< / script>
("#__REQUESTDIGEST").val(), 'content-type': 'application/json;odata=verbose', 'accept': 'application/json;odata=verbose' }, success: function (data) { console.log(data); console.log(data.d.results.length); }, error: function (data) { ///do your code } }); } </script>





谢谢

最好的问候


这篇关于无法通过在REST中传递CAML查询来获取总项数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆