I have the below code which grabs all items from a List with the following structure:

enter image description here

I want to limit the query results by a specific value of 'ID' (so when the code gets to the enumerator part, I don't have to change that code, as the enumerator will only run once - the specific value of ID to be used will be generated by code elsewhere but for now just need it to work with a manual value).

I've tried a fair few different ways from blog posts and questions here but I can't seem to get it to work - either I still get all items in the List, or none at all.

Any suggestions? Code I'm using is below:

<div id="companiesSpace">Change me</div>

<script type="text/javascript">
var clientContext;
var oWeb;
var companiesList;
var allCompanies;
var companiesMarkupBlock = "<b>Companies</b><br />"

function getCurrentList(listName) {
      thisList = oWeb.get_lists().getByTitle(listName);
      return thisList;

function runOnFail() {
    alert("Failed to get client context");

function runOnSuccess() {
    var camlQuery = new SP.CamlQuery();
    var camlQueryString = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
    allCompanies = companiesList.getItems(camlQuery);

function runOnSecondFail() {
    alert("Failed to issue CAML query");

function runOnSecondSuccess() {
  var companyEnum = allCompanies.getEnumerator();
  while (companyEnum.moveNext()) {
      var currentCompany = companyEnum.get_current();
      var thisCompanyId = currentCompany.get_item('ID');
      var thisCompanyName = currentCompany.get_item('companyName');
      companiesMarkupBlock += thisCompanyId;
      companiesMarkupBlock += " : ";
      companiesMarkupBlock += thisCompanyName;
      companiesMarkupBlock += "<br />";
  document.getElementById('companiesSpace').innerHTML = companiesMarkupBlock;

function getContacts() {
    clientContext = SP.ClientContext.get_current(); 
    oWeb =  clientContext.get_web();
    companiesList = getCurrentList('Companies');


1 Answer 1



var camlQueryString = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";


var camlQueryString = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{ADD LIST ITEM ID HERE}</Value></Eq></Where></Query></View>";
  • Doesnt work I'm afraid - when I put '1' in as the value between <Value> tags to get the item shown above, it still returns all items. Jul 24, 2017 at 9:32
  • 2
    Sorry, updated the answer. You need to have <View> tags as well since you are using CSOM / JSOM Jul 24, 2017 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.