0

I'm using the following query :

 camlQuery.set_viewXml = "<View><Query><Where><Eq><FieldRef Name='Employee'/><Value Type='User'>" + currentUser.get_title() + "</Value></Eq></Where></Query></View>"; var listItemCollection = list.getItems(camlQuery); clientContext.load(listItemCollection); clientContext.executeQueryAsync(function () { //alert("Success"); var listItemEnumerator = listItemCollection.getEnumerator(); if (listItemCollection.get_count() == 1 && listItemEnumerator.moveNext()) { } 

This query returns ok results (count = 1) using the U2U Caml Query builder. When i use the code in visual studio it returns 2 results, all the items in the list.

What is wrong with my code?

I also tried query with ID but again returns all items.

 //camlQuery.set_viewXml = ("<Query><Where><Eq><FieldRef Name='Employee' LookupId='True'/><Value Type='Lookup'>" + _spPageContextInfo.userId + "</Value></Eq></Where></Query>"); 

1 Answer 1

0

Found the issue :

Problem is using camlQuery.set_ViewXml with hardcoded the query.

If i use this it works :

var ViewXml = "<View><Query><Where><Eq><FieldRef Name='Employee' LookupId='True'/><Value Type='Lookup'>" + _spPageContextInfo.userId + "</Value></Eq></Where></Query></View>"; //var ViewXml = "<View><Query><Where><Eq><FieldRef Name='Employee' LookupId='True'/<Value Type='Lookup'>" + currentUser.get_title() + "</Value></Eq></Where></Query></View>"; camlQuery.set_viewXml(ViewXml); 
1
  • The main reason is you were missing ( and ) brackets around the caml query (refer your question). Just update the line and it will work. No need to write two seperate lines. Try Below line, it will also work -- camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Employee'/><Value Type='User'>" + currentUser.get_title() + "</Value></Eq></Where></Query></View>"); Commented Nov 10, 2017 at 12:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.