Question

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.

Was it helpful?

Solution

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
}

OTHER TIPS

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");
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top