1

I have been trying to work on this for a few hours and a lot of googling. New to caml queries I simply want to search my list where the variable matches in the column.

In sql I would write:

sql= SELECT COL1 FROM Table WHERE COL1 = '"+ VAR1 +"';

I am trying to retrieve the ID of the result.

function GetProdIDbyName(){
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyListName');   
var camlQuery = new SP.CamlQuery();


camlQuery.set_viewXml("<View><Query><Eq><FieldRef Name='Prod_x0020_Name' /><Value Type='Text'>" + ProdName + "</Value></Eq></Query></View>");   
this.collListItem = oList.getItems(camlQuery)   
clientContext.load(collListItem);
clientContext.executeQueryAsync(ProdIDSucceeded, ProdIDFailed);

}

function ProdIDSucceeded(sender, args) {

var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nProd Value: ' + oListItem.get_item('Product_x0020_No');

}

alert(listItemInfo.toString());
}

function ProdIDFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

But my results ignore the variable "ProdName" and returns all data in the column. I read this is an issue with the caml string.

Some advice and guidance would be much appreciated. Thank you.

0

2 Answers 2

0

I have added one line in your code as context.load(collListItem, 'Include(---- Column name which you need ---)');

Please check it.

function GetProdIDbyName(){
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyListName');   
var camlQuery = new SP.CamlQuery();


camlQuery.set_viewXml("<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + ProdName + "</Value></Eq></Where></Query>");

this.collListItem = oList.getItems(camlQuery);
context.load(collListItem, 'Include(---- Column name which you need ---)');

clientContext.load(collListItem);
clientContext.executeQueryAsync(ProdIDSucceeded, ProdIDFailed);

}

function ProdIDSucceeded(sender, args) {

var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nProd Value: ' + oListItem.get_item('Product_x0020_No');

}
ProdID = oListItem.get_id();
ProdName = oListItem.get_item();

alert(RegID + " " +ProdName);
}

function ProdIDFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
13
  • should it be : clientContext.load(collListItem, 'Include(ID)'); Commented Apr 8, 2016 at 10:30
  • i already mentioned that "Column name which you need". You will get it in output Commented Apr 8, 2016 at 10:36
  • Its not working it errors saying : The property or field 'Id' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. Commented Apr 8, 2016 at 10:54
  • You need to load oListItem in ProdIDSucceeded method after var oListItem = listItemEnumerator.get_current(); Like clientContext.load(oListItem); Commented Apr 8, 2016 at 10:56
  • This has confused me as in my first scenario the code works fine and gets the results I need. on debugging the error appers after the while ProdID = oListItem.get_id(); ProdName = oListItem.get_item(); Commented Apr 8, 2016 at 11:50
0

Yay after much trail and error I found a solution:

If looking to search Sharepoint list by variable using CAML & Javascript then the query string below will bring back id (or value if dropdown etc) and field data. The code in my question hasn't changed except the query string:

camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\"Prod_x0020_Name\"></FieldRef><Value Type=\"Text\">"+ ProdName +"</Value></Eq></Where></Query></View>");

Hope this helps others :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.