-1
var fieldName;


 var listFields;
    var CmalQuery;
    $(document).ready(function () {

        $('#btnSearch').on('click', function () {
            txtValue = $('#txtSearch').val();
            ListName =   $('#ddlTitles').val();

             retrieveFieldsOfListView(ListName);

    });
     $('#btnList').on('click', function () {
        GetAllLists();
    });
    /* $('select').on('change', function () {
       ListName = this.value;
       retrieveFieldsOfListView(ListName);
       txtValue = $('#txtSearch').val();
         // or $(this).val()
    });*/    
    });




function GetListData() {


    var context = SP.ClientContext.get_current();
    var user = context.get_web().get_currentUser();

    //Get the list. Alter this code to match the name of your list  
    var list = context.get_web().get_lists().getByTitle(ListName);
    //  context.executeQuerySync(printFieldNames, onError);

    //Create a new CAML query  

    var caml = new SP.CamlQuery();
     // CmalQuery="<View><Query><Where><Or><Contains><FieldRef Name='LinkTitle' /><Value Type='Text'>p</Value></Contains><Contains><FieldRef Name='EmpName' /><Value Type='Text'>p</Value></Contains></Or></Where></Query></View>";   //Create the CAML that will return only items with the titles that contains with 'txtValue' 
     caml.set_viewXml(CmalQuery);
    //Specify the query and load the list oject  
      returnedItems = list.getItems(caml);
      context.load(returnedItems);
    //Run the query asynchronously, passing the functions to call when a response arrives  
    context.executeQueryAsync(onQuerySuccess, onQueryFail);
    function onQuerySuccess(sender, args) {

        var enumerator = returnedItems.getEnumerator();
        //Formulate HTML from the list items  
        var markup = 'Items in the Employee Details list that contains with ' + '"' + txtValue + '"' + ': <br><br><table class="table table-striped">';
               //Loop through all the items  
        while (enumerator.moveNext()) {
            var listItem = enumerator.get_current();
            markup += '<tr><td>' + listItem.get_item(fieldName) + '</td></tr>';
             //markup += '<td>' + listItem.get_item('JobTitle') + '</td>';
            //markup += '<td> ' + listItem.get_item('Title') + '</td> ';
           //markup += '<td>' + listItem.get_item('ManagerName').get_lookupValue() + '</td></tr>';
          //Display the formulated HTML in the displayDiv element  
        }
        markup += '</table>'
        $('#resultsDiv').html(markup);
    }
    function onQueryFail(sender, args) {//Formulate HTML to display details of the error  
        var markup = '<p>The request failed: <br>';
        markup += 'Message: ' + args.get_message() + '<br>';
        //Display the details  
        $('#resultsDiv').html(markup);
    }


}

function GetAllLists() {
    var context = SP.ClientContext.get_current();
    var user = context.get_web().get_currentUser();
    var oWebsite = context.get_web();
    this.collList = oWebsite.get_lists();
    context.load(collList);

    context.executeQueryAsync(onQuerySucceeded,onQueryFailed);
    function onQuerySucceeded() {
        var listInfo = '<table class="table table-striped"><tr>';
        var listEnumerator = collList.getEnumerator();

        while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();

            listInfo += '<tr><td>Title: ' + oList.get_title() + '</td></tr>';

           $("#ddlTitles").append($("<option></option>").html(oList.get_title()));
        }
        listInfo +='</tr></table>'
         $('#resultsDiv').html(listInfo);



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


function retrieveFieldsOfListView(ListName) {

    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(ListName);
    var view = list.get_views().getByTitle('All Items');
    listFields = view.get_viewFields();
    context.load(listFields);
    context.executeQueryAsync(printFieldNames, onError);


    function printFieldNames() {
    var e = listFields.getEnumerator();
        CmalQuery = "<View><Query><Where><Or>";
        while (e.moveNext()) {
            fieldName = e.get_current();

           CmalQuery += "<Contains><FieldRef Name='"+fieldName+"' /><Value Type='Text'>" + txtValue + "</Value></Contains>";

            console.log(fieldName);

        }
          CmalQuery += "</Or></Where></Query></View>";
          GetListData();
    }

    function onError(sender, args) {
        console.log(args.get_message());
    }
    } 

closed as unclear what you're asking by Atish Dipongkor - MVP, Karthik Jaganathan, Benny Skogberg Jun 22 '16 at 11:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

Replace following block from your code

CmalQuery = "<View><Query><Where><Or>";
        while (e.moveNext()) {
            fieldName = e.get_current();

           CmalQuery += "<Contains><FieldRef Name='"+fieldName+"' /><Value Type='Text'>" + txtValue + "</Value></Contains>";

            console.log(fieldName);

        }
          CmalQuery += "</Or></Where></Query></View>";

and add this block which will build dynamic query

var counter = 0;

CmalQuery = "<View><Query><Where>";
        while (e.moveNext()) {
            fieldName = e.get_current();

           if(counter >0)
               CmalQuery = "<Or>" + CmalQuery;

           CmalQuery += "<Contains><FieldRef Name='"+fieldName+"' /><Value Type='Text'>" + txtValue + "</Value></Contains>";

           if(counter > 0)
               CmalQuery =  CmalQuery + "</Or>";
            counter++;

            console.log(fieldName);

        }
          CmalQuery += "</Where></Query></View>";
  • You can accept as answer or upvote if helped you.. so people looking for same will be helped – Gaurravs Jun 23 '16 at 12:13

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