3

I have a SharePoint Application that needs to data from lists to be inserted into elements.

Here is the JavaScript:

Collection.List.DropDown = function (listTitle, element) {
//listTitle = "DropDownLists";

var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle("DropDownLists");

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml("   <Where><Eq><FieldRef Name='ListType' /><Value Type='Text'>Customer Class</Value></Eq></Where>");

                        /*<ViewFields>" +
                            "<FieldRef Name='ID' />" +
                            "<FieldRef Name='ListType' />" +
                            "<FieldRef Name='Title' />" +
                            "<FieldRef Name='ListDefault' />" +
                        "</ViewFields>" +
                        "<OrderBy>" +
                            "<FieldRef Name='ListType' />" +
                            "<FieldRef Name='Title' />" +
                        "</OrderBy>" +
                        "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name='ListType' />" +
                                "<Value Type='Text'>" + listTitle + "</Value>" +
                            "</Eq>" +
                        "</Where>");

                        /*"<Query>" +
                            "<Where>" +
                                "<Eq>" +
                                    "<FieldRef Name=\"ListType\"/>" +
                                    "<Value Type=\"Text\">\"" + listTitle + "\"</Value>" +
                                "</Eq>" +
                            "</Where>" +
                            "<OrderBy>" +
                                "<FieldRef Name=\"ListValue\"/>" +
                            "</OrderBy>" +
                        "</Query>");*/



var collListItem = oList.getItems(camlQuery);

//clientContext.load(this.collListItem, "Include(Id, ListType, ListValue, ListDefault)");

clientContext.load(collListItem);

clientContext.executeQueryAsync(
    function (sender, args) {
        var listItemInfo = '';
        var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += "<option id=\"" + oListItem.get_id() + "\" " + (oListItem.get_item("ListDefault") == true ? " selected=\"selected\"" : "") + ">" + oListItem.get_item("Title") + "</option>";
        }
        document.getElementById(element).innerHTML = listItemInfo.toString();

    },
    function (sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    });

};

The lists return successfully, but the CAML Query is not working, it keeps returning all the items withing the list.

In the code example you can see all the different CAML Queries I tried.

Thanks in advance.

1
  • 2
    Try starting the caml query within tags <View><Query><Where><Eq>.
    – Prakash
    Nov 19 '15 at 9:47
3

Please see this example from MSDN, you need to wrap with <View> tags, as shown below:

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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