1

I want to retrieve a record from SharePoint list by passing field value to CAML query using javascript,I tried below code

function retrieveDetails(name) {

    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context      object based upon the URL
    var myList = ctx.get_web().get_lists().getByTitle("PersonalDetails");
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Query><Where><eq><FieldRef Name=\'Name\'/><Value Type=\'Text\'>' + name + '</Value><FieldRef Name=\'email\'/><Value Type=\'Text\'></Value></eq>" + "</Where></Query><RowLimit>100</RowLimit></View>');
    var items = myList.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    function onQuerySucceeded() {
        Roomdetailstable(items);
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }
}
function details(items) {
            var itemInfo = '';       
            var enumerator = items.getEnumerator();       
            while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();     
            var rnmae = currentListItem.get_item('Name');
            document.getElementById("rnme").innerHTML = rnmae ;
            var emailid= currentListItem.get_item('email');
            document.getElementById("emailid").innerHTML = emailid;

        }
    }  


but it is showing error like below enter image description here

  • 1
    Have you tested your caml query with something like the u2u caml query builder: u2u.be/Software ? – Oak3 Aug 24 '16 at 12:12
  • your query seems to be wrong. You are using two fields in the query but there is no And/Or operator in your query. As suggested by @MartijnE, use CAML query builder to build the query – Nadeem Yousuf Aug 24 '16 at 12:15
2

resolved this issue by removing <Query></Query> from my caml query

function retrieveDetails(name) {

    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context      object based upon the URL
    var myList = ctx.get_web().get_lists().getByTitle("PersonalDetails");
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Where><eq><FieldRef Name=\'Name\'/><Value Type=\'Text\'>' + name + '</Value><FieldRef Name=\'email\'/><Value Type=\'Text\'></Value></eq>" + "</Where><RowLimit>100</RowLimit></View>');
    var items = myList.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    function onQuerySucceeded() {
        Roomdetailstable(items);
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }
}
function details(items) {
            var itemInfo = '';       
            var enumerator = items.getEnumerator();       
            while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();     
            var rnmae = currentListItem.get_item('Name');
            document.getElementById("rnme").innerHTML = rnmae ;
            var emailid= currentListItem.get_item('email');
            document.getElementById("emailid").innerHTML = emailid;

        }
    }  
0

Your caml query is wrong. If you want to be sure it's not the code, please try this caml query:

query.set_viewXml('<View><Query><Where><IsNotNull><FieldRef Name=\'ID\' /></IsNotNull></Where></Query><RowLimit>100</RowLimit></View>');
0

Try below code to get the data from SharePoint list using CAML query.

function retrieveDetails(name) {

    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context      object based upon the URL
    var myList = ctx.get_web().get_lists().getByTitle("PersonalDetails");
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Query><Where><eq><FieldRef Name=\'Name\'/><Value Type=\'Text\'>' + name + '</Value><FieldRef Name=\'email\'/><Value Type=\'Text\'></Value></eq>" + "</Where></Query><RowLimit>100</RowLimit></View>');
    var items = myList.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );


}

function onQuerySucceeded() {
        var itemInfo = '';       
            var enumerator = items.getEnumerator();       
            while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();     
            var rnmae = currentListItem.get_item('Name');
            document.getElementById("rnme").innerHTML = rnmae ;
            var emailid= currentListItem.get_item('email');
            document.getElementById("emailid").innerHTML = emailid;
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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