Solved

JSOM query for just 1 item

Posted on 2016-11-10
9
46 Views
Last Modified: 2016-11-20
What if my query returns one item every time?  When i remove the while loop, it does not work.  Thanks

var ctx = new SP.ClientContext.get_current();
var customList = ctx.get_web().get_lists().getByTitle('CustomList');
 
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query> <Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq></Where></Query></View>');
var listItemCol = customList.getItems(camlQuery);
 
ctx.load(listItemCol);
 
ctx.executeQueryAsync(  
    function(){ 
        var listItems = [];
        var listEnum = listItemCol.getEnumerator();
         
        while (listEnum.moveNext()) {
            var item = listEnum.get_current();
            listItems.push('ID: ' + item.get_id() + ' Title: ' + item.get_item('Title'));
        }
         
        alert(listItems.join("\n")); 
    }, 
    function(sender, args){ alert('Error: ' + args.get_message()); }
);

Open in new window

0
Comment
Question by:Isaac
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 41883038
this is not an issue. you still receive an array... maybe with one object inside but still an array so your current have sense.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41883110
Sorry.what about the code below?  I posted the wrong one.

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    
 while (listItemEnumerator.moveNext()) {
    
    var oListItem = listItemEnumerator.get_current();
              
                 $('#visn').val(oListItem.get_item('VISN'));
                $('#projTitle').val(oListItem.get_item('Title'));
                $('#station').val(oListItem.get_item('Station_x0020_Number'));
                $('#stationName').val(oListItem.get_item('Station_x0020_Name'));        
                 $('#projNumber').val(oListItem.get_item('Project_x0020_Number'));   
}              
      
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 41883116
I think the problem should be when you've more than one because you're using the same set textboxes to show the value
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 5

Author Comment

by:Isaac
ID: 41884876
The problem occurs when I have one set returned.  I have no issues with multiple returns
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41884895
Could you describe your problem?
0
 
LVL 5

Author Comment

by:Isaac
ID: 41884899
I am using caml query to query my sharepoint list for just 1 item but I get the error noted in one of my comments above.
I am using the caml above which filters the list if "ID" is equal to 1.  When I remove the line below, I get data back data but that's not what I want. I only want the line item where id=1.

camlQuery.set_viewXml('<View><Query> <Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq></Where></Query></View>');

Open in new window


Hope that makes sense.

Thanks!
0
 
LVL 5

Accepted Solution

by:
Isaac earned 0 total points
ID: 41884904
Never mind.  I've figured out the problem.  My query was wrong.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41884953
You can close the question accepting your own comment as solution
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41894669
Issue resolved
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
show form submit button based on jquery sum 5 28
SharePoint Explorer Folder Access 4 36
Help with removing item from Session Array 3 23
MVC DDL Json Not Binding to Model 2 13
Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question