Tuesday, March 15, 2016

Get All Lists, List Items & Bind List Items to JQUery DataTable REST API

1. Get All Lists in a SharePoint 2013 using REST API

2. Get Top ListItems
3. Display ListItems in JQUery DataTable


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript"  src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript"  src="/_layouts/15/sp.runtime.js"> </script>

<script type="text/javascript"  src="/_layouts/15/sp.js"> </script>

=======================================================
<div>
    <input type="button" value="Get All List" onclick="getAllList();" />
</div>

<script type="text/javascript">

    // Get All Lists in Site
    function getAllList() {
        debugger;
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists";
                        //_api/Lists/GetByTitle('Informations')/items?$select=EncodedAbsUrl,Category,ID&$orderby=Category asc&$orderby=Modified desc
        var requestHeaders = { "accept": "application/json;odata=verbose" };
        $.ajax({
            url: requestUri,
            contentType: "application/json;odata=verbose",
            headers: requestHeaders,
            success: onSuccess,
            error: onError
        });
        function onSuccess(data, request) {
            var listColl ="";
            var myLog = data.d;

            for (var resultTipe in myLog.results) {
                listColl += myLog.results[resultTipe].Title +"\n";
            }
            alert(listColl);
        } 
          
    
        function onError(error) {
            alert("error");
        }

    }
=====================================================================
2. Get Top ListItems

        <input id="txtItemToShow" type="text" />

        var item2Show = $("#txtItemToShow").val();



       var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('rku list')/items?select=id,Title&$Top="+ item2Show+" ";

=======================================================================

3. Get ListItems and Display in JQuery DataTable



<script type="text/javascript"  src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript"  src="/_layouts/15/sp.runtime.js"> </script>
<script type="text/javascript"  src="/_layouts/15/sp.js"> </script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript"  src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">


<section>
<div id="divUserNewStarter">
   
<style type="text/css">
       #divUserNewStarter {font-size:12px;}
       #divUserNewStarter li {color:rgb(182,182,182);}
</style>

<table id='tblAllItems'>
       <thead>
       <tr >
           <th align="Left">Item ID</th>
           <th align="Left">Title</th>                
       </tr>
       </thead>
              <tbody>
              </tbody>
</table>     
</div>
</section>

<script type="text/javascript">
  
    $(document).ready(function () {               
        getAllListItems();       
    });
  
  
    function getAllListItems() {         
      
         item2Show = 110;
      
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('rku list')/items?select=id,Title&$Top=" + item2Show + " ";
        var requestHeaders = { "accept": "application/json;odata=verbose" };

        $.ajax({
            url: requestUri,
            contentType: "application/json;odata=verbose",
            headers: requestHeaders,
            success: onSuccess,
            error: onError
        });
    }

    function onSuccess(data, request) {
        riskTable = "";
        var myLog = data.d;

        for (var resultType in myLog.results) { 
            riskTable = "<tr class='rowStyle'><td><li class='tt-ListSquare'><span><a class='tt-Anchor'  target=_self href=" + _spPageContextInfo.webAbsoluteUrl + "_layouts/15/listform.aspx?PageType=4&ListID=2d90bd86-b9f9-4a1a-aa5b-59e4f632fc8f&ID=" + myLog.results[resultType].ID + "&source=" + _spPageContextInfo.webAbsoluteUrl + ">" + myLog.results[resultType].Title + "</a></span></li></td>";
          
            riskTable = riskTable + "<td><span>" + myLog.results[resultType].ID + "<span></td>";
            $('#tblAllItems').append(riskTable);
        }
      
       // $("#countTable").dataTable({ "bSort": false });
       //  $("#countTable").dataTable({ "iDisplayLength": 5 });
       //  bFilter: True Enables the search
        $("#tblAllItems").dataTable({
            "bPaginate": true,
            "bInfo": true,
            "bFilter": true,
            "bLengthChange": false,
            "bSort": false
        });      

    }

    function onError(error) {
        alert("error");
    }

</script>


No comments:

Post a Comment