0

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.

UPDATE:

I tried just querying with:

'<View><Query></Query></View>'

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();
camlQuery.set_viewXml();
this.collListItem = oList.getItems(camlQuery);

And I get the same result.

1
  • 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? Aug 15, 2016 at 14:50

3 Answers 3

0

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.load(dview);
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
}
4
  • clientContext.load(dview) is causing an error here.
    – Ulthran
    Aug 15, 2016 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 Aug 15, 2016 at 17:30
  • It's undefined... Which is weird because it should be defined in this line: var dview = oList.DefaultView;
    – Ulthran
    Aug 15, 2016 at 17:34
  • as i said you need to request for that as part of clientContext.load(oList, 'Include(DefaultView)'... Aug 15, 2016 at 17:42
0

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


function onQuerySucceeded(sender, args) {   
    alert("SUCCESS");
    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);

    }       
    alert("FIN");
}

1
  • This is the process which is only returning the last value.
    – Ulthran
    Aug 15, 2016 at 14:19
-1

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.

3
  • Adding in these fields makes the Query return nothing.
    – Ulthran
    Aug 15, 2016 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, 2016 at 4:25
  • I didn't actually downvote your answer, but your answer still doesn't work for me.
    – Ulthran
    Aug 16, 2016 at 12:12

Your Answer

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

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