[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

JSOM query for just 1 item

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
Isaac
Asked:
Isaac
  • 5
  • 4
1 Solution
 
leakim971PluritechnicianCommented:
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
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
IsaacSharePoint Client Side DeveloperAuthor Commented:
The problem occurs when I have one set returned.  I have no issues with multiple returns
0
 
leakim971PluritechnicianCommented:
Could you describe your problem?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
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
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Never mind.  I've figured out the problem.  My query was wrong.
0
 
leakim971PluritechnicianCommented:
You can close the question accepting your own comment as solution
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Issue resolved
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now