0

As the title says, I have a caml query that is invoked through JSOM code which always returns an empty collection. I am querying the list based on two columns: One being of type User and the other of being of type Text.

Here are the snipps:

$("#searchUser").click(function () {
    var _BizRepo = Biz.Services.SearchRepository;
    var currentCtx = SP.ClientContext.get_current();
    var picker = SPClientPeoplePicker.SPClientPeoplePickerDict.clientPeoplePicker_TopSpan.GetAllUserInfo();
    var user = picker[0].Key;
    var selectedYear = $("#year").val();
    var metricsRaw = _BizRepo.GetMetricsForUser(currentCtx, user, selectedYear, "Test");

    if (metricsRaw === undefined) { //Always end up in this block
        $("#NoResults").html("No results found.");
    }
 //ommitted for brevity...

GetMetricsForUser() does the actual querying.

GetMetricsForUser: function (currentCtx, user, selectedYear, docLibName) {
            var metricsList = currentCtx.get_web().get_lists().getByTitle("Metrics for " + docLibName);
            var query = new SP.CamlQuery();
            query.set_viewXml("<Where><And><Eq><FieldRef Name='User' /><Value Type='User'>" + user + "</Value></Eq><Eq><FieldRef Name='Year' /><Value Type='Text'>" + selectedYear + "</Value></Eq></And></Where>");
            var itemCollection = metricsList.getItems(query);
            currentCtx.load(itemCollection);
            currentCtx.executeQueryAsync(function () { return itemCollection; }, function (sender, args) { console.log(args.get_message()); });
        }

No exceptions are thrown when stepping through the code, so i'm completely lost. Am I passing an incorrect user type of object to the query perhaps?

1
  • 1
    I believe you need in your set_viewXml an opening <View><Query> and a closing </Query></View> Oct 24, 2015 at 3:31

1 Answer 1

1

You are missing View and Query tags in your query. Try including <View><Query><Where></Where></Query></View> in your set_ViewXml.

Also, the preferred way of querying the user field is :

<Where><Eq>
   <FieldRef Name='User' LookupId='True'  />
   <Value Type='User'>Id</Value>
</Eq></Where>

Since it ensures that the result returned is unique.

1
  • Added in the opening and closing <View><Query> tags in my query and my collection is still undefined when stepping through. Marking this as answer since it seems to have solved my caml issue. Perhaps you could help me on my follow up question too? sharepoint.stackexchange.com/q/160548/31470
    – KidBatman
    Oct 25, 2015 at 18:27

Your Answer

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

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