使用对象的set_viewXml方法SP.CamlQuery指定CAML查询以检索项目。
SP.SOD.executeOrDelayUntilScriptLoaded(showListItems,"core.js"); function showListItems(){ var clientContext = new SP.ClientContext(); var list = clientContext.get_web().get_lists().getByTitle("List Title"); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml( "<View><Query>" + "<Where>" + "<Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">Value</Value></Eq>" + "</Where>" + "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\"/></OrderBy>" + "</Query>"+ //"<RowLimit>5000</RowLimit>" + "</View>"); var items = list.getItems(camlQuery); clientContext.load(items); clientContext.executeQueryAsync(function(){ var itemArray = []; var itemEnumerator = items.getEnumerator(); while(itemEnumerator.moveNext()){ var item = itemEnumerator.get_current(); var id = item.get_item("ID"); var title = item.get_item("Title"); itemArray.push(id + ": " + title); } alert("ID: Title\n"+itemArray.join("\n")); },function(sender,args){alert(args.get_message());}); }
您可以利用RowLimitCAML查询中的元素在每个查询中仅检索结果的子集。
使用get_listItemCollectionPosition列表项集合的方法检索当前位置,然后将该值用作SP.CamlQuery对象set_listItemCollectionPosition方法中的参数以检索下一批结果。
SP.SOD.executeOrDelayUntilScriptLoaded(showListItems,"sp.js"); function showListItems(){ var itemArray = []; var clientContext = new SP.ClientContext(); var list = clientContext.get_web().get_lists().getByTitle("List Title"); var viewXml = "<View><Query>" + "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\"/></OrderBy>" + "</Query>"+ "<RowLimit>1</RowLimit>" + "</View>"; var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(viewXml); var items = list.getItems(camlQuery); clientContext.load(items); clientContext.executeQueryAsync(loadResults,showError); function loadResults(){ var resultsFound = false; var itemEnumerator = items.getEnumerator(); while(itemEnumerator.moveNext()){ var item = itemEnumerator.get_current(); var id = item.get_item("ID"); var title = item.get_item("Title"); itemArray.push(id + ": " + title); } var pos = items.get_listItemCollectionPosition();// <- get position if(pos !== null){ // <-- position is null when no more results are returned if(confirm("Results so far: \nID: Title\n"+itemArray.join("\n"))){ camlQuery = new SP.CamlQuery(); camlQuery.set_listItemCollectionPosition(pos);// <- set position for next batch camlQuery.set_viewXml(viewXml); items = list.getItems(camlQuery); clientContext.load(items); clientContext.executeQueryAsync(loadResults,showError); } }else{ alert("Total Results: \nID: Title\n"+itemArray.join("\n")); // <- display when no more results } } function showError(sender,args){ alert(args.get_message()); } }