I have a CamlQuery which I set as follows:

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

'Part Name' is an existing field in the table on which I'm running the query, it is a text field, and the variable partName contains a string which is included in this field in the table.

However, the query returns the last item in the table instead of this one (one of the first items, not that it matters).

I've tried deleting the last item to see if there was something specific about that one but it happened again with the new last item.

I'm new to caml so I don't know what it going wrong with this.

Any help would be appreciated.


I tried just querying with:


And this returned the same result, just the last item. I don't think the query is even being used. I can even write:

var camlQuery = new SP.CamlQuery();
this.collListItem = oList.getItems(camlQuery);

And I get the same result.

  • did you tried to create a normal view and use the filtering so you can check if the filter criteria matches your item result count? – Marcelo Espinosa Aug 15 '16 at 14:50

Open the list in browser and check the default view. How many items is it returning?

Now try this in your code. Basically its trying to mimic what you observed in the default view

//Load default view
var dview = oList.DefaultView;
clientContext.executeQueryAsync(Function.createDelegate(this, loadedView), Function.createDelegate(this, onQueryFailed));

function loadedView() {
    var query = new SPQuery();
    query.Query = dview.get_viewQuery();
    var items = oList.GetItems(query);
    //... load items and execute query
| improve this answer | |
  • clientContext.load(dview) is causing an error here. – Ulthran Aug 15 '16 at 17:22
  • Can you check if dview is having a null value? You might need to load it as part of loading the oList object.. use Include – Amal Hashim Aug 15 '16 at 17:30
  • It's undefined... Which is weird because it should be defined in this line: var dview = oList.DefaultView; – Ulthran Aug 15 '16 at 17:34
  • as i said you need to request for that as part of clientContext.load(oList, 'Include(DefaultView)'... – Amal Hashim Aug 15 '16 at 17:42

You have to iterate trough the Enumerator, check the following code:

function onQuerySucceeded(sender, args) {   
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        var iden = oListItem.get_item("ID");
        alert("id: " + iden);           
        var proyecto = oListItem.get_item("Project");
        alert("proyecto: " + proyecto);

        var nombre = oListItem.get_item("ProjectName");
        alert("name: " + nombre);

        var activo = oListItem.get_item("Active");
        alert("active: " + activo);


| improve this answer | |
  • This is the process which is only returning the last value. – Ulthran Aug 15 '16 at 14:19

You are missing <Query> and <View> in query passed in camlQuery.set_viewXml().. Try this query:

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

And refer this MSDN link for further reference.

| improve this answer | |
  • Adding in these fields makes the Query return nothing. – Ulthran Aug 15 '16 at 14:17
  • As per MSDN link, you have to add <Query> and <View> in set_viewXml().. So i have corrected you query.. so @Ulthran : you can't down vote my answer as i have give a positive answer.. – SID Aug 16 '16 at 4:25
  • I didn't actually downvote your answer, but your answer still doesn't work for me. – Ulthran Aug 16 '16 at 12:12

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.