Thursday, December 26, 2013

SharePoint 2013 Rest API Search through List and filter by multiple columns


(1) In the Webpart page, add a script editor webpart
(2) Paste the code shown below in the Editor.
(3) Search the Columns  ( * Filter by word containging the searched word in Column1 or Column2)
(4) Results shown in DataTable
(5) Search results can be filtered through another searchbox.
(6) Changes to be made are Highlighted in blue



Original Post by: Mark Rackley
http://summit7systems.com/who-needs-a-data-view-web-part-sharepoint-rest-and-datatables-net/


SearchList
Search through a list by Column1 or Column2



<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">

<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
       <input type="text" id="techsearch" >
       <input type="button" value="Search" onclick="LoadTechnologies($('#techsearch').val());" >




<table width="100%" cellpadding="0" cellspacing="0" border="0" class="display" id="TechTable">
    <thead>
        <th>Name of the Technology</th>
        <th>Vendor</th>
        <th>Green Listed</th>

    </thead>
</table>


<script type="text/javascript">

     function LoadTechnologies(techsearch)
      {
      
       alert("Searching by: " + techsearch + " through Technology Title and Vendor in List Name");
       var call = $.ajax(
                       {
                         url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ListName')/items?$select=Title,Vendor,GreenList&$filter=(substringof('" + techsearch + "', Title) or substringof('" + techsearch + "', Vendor))",
                         type: "GET",
                         dataType: "json",
                         headers: { Accept: "application/json;odata=verbose" }
                       }
        );// end ajax

      call.done(function (data,textStatus, jqXHR)
       {
         $('#TechTable').dataTable(
                       {
                        "bDestroy": true,
                        "bProcessing": true,
                        "aaData": data.d.results,
                        "aoColumns": [ { "mData": "Title" }, { "mData": "Vendor" }, { "mData": "GreenList" } ]
                        }); //end dataTable
       }); //end call done function
     
       call.fail(function (jqXHR,textStatus,errorThrown){ alert("Error retrieving Tasks: " + jqXHR.responseText); }); }
</script>

No comments: