2

I want to match the current user with a lookup field in a specific list. It seems something is wrong with my caml query.

var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var list = web.get_lists().getByTitle("Managers");
    var currentUser = context.get_web().get_currentUser();
    var currentUserLoginName = currentUser.get_loginName();
    var currentUserId = currentUser.get_id();
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Employee\'LookupId=\'True\'>' +
        '<Value Type=\'Lookup\'>' + currentUserId + '</Value></Eq></Where></Query></View>');

    var listItemCollection = list.getItems(camlQuery);

    clientContext.load(listItemCollection);

Where is my mistake?

Thanks a lot !

2
  • what's the error you see? Oct 16, 2017 at 6:39
  • Actually there isn't any error . It just won't return any results although i have records with the lookup field value to current user
    – shak
    Oct 16, 2017 at 6:41

2 Answers 2

1

Try this code:

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("ListName");
var cQuery = new SP.CamlQuery();
var camlXML = "<View><Query><Where><Eq><FieldRef Name='Author' LookupId='True'/><Value Type='Lookup'>" + _spPageContextInfo.userId + "</Value></Eq></Where></Query></View>";
cQuery.set_viewXml(camlXML);
var listitems = list.getItems(cQuery);
context.load(listitems);
context.executeQueryAsync(function() {
        var enumerator = listitems.getEnumerator();
        while (enumerator.moveNext()) {
            var item = enumerator.get_current().get_fieldValues();
            console.log(item);
        }
    },
    function(s, a) {
        console.error(a.get_message());
    });
0

To match current user you can directly use below CAML query :

<Where>
  <Eq>
    <FieldRef Name="Employee" />
    <Value Type="Integer">
      <UserID Type='Integer'/>
    </Value>
  </Eq>
</Where>

I have made some changes to your code. Refer below code. This should work :

function getData(){

    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Managers");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Employee' /><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Where></Query></View>");
    listItemCollection = list.getItems(camlQuery);

    clientContext.load(listItemCollection);
    clientContext.executeQueryAsync(getDataSuccess, getDataFailed);

}

function getDataSuccess(){

    var listItemEnumerator = listItemCollection.getEnumerator();
    while(listItemEnumerator.moveNext()){
         var oListItem = listItemEnumerator.get_current();

         // further code 

    }
}

function getDataFailed(sender, args){

    // failure handler

}

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.