Monday, March 07, 2016

jQuery ajax method and SharePoint list

Using jQuery ajax method we can initiate the RESTful request to retrieve the SharePoint List Items that has been designated as resources through end point.

Once the end point has been identified, the query will be used to make CRUD operation using OData protocol.

Here is the simple code snippet and bit explanation behind the jQuery ajax call and the call back method.

All you need to is to pass the SharePoint's REST endpoint URI as url parameter to ajax method.
And request what data format has to be returned by the request by specifying the header format.


 $(document).ready(function () {
            $.ajax(
                {

                   url: "http://win-0nvq2rjqgo2/_api/web/lists/getbytitle('trac')/items",
                    mehtod: "Get",
                    headers: { "Accept": "application/json; odata=verbose" },
                   dataType:"json",
                  success: function (data) {
                        SuccessMessage(data)
                    },

                     error: function (data) {
                     //var parsedJson = $.parseJSON(data.responseText);
                       console.log(data.responseText);
                        //Logging the error details
                  }
                });
        });


        function SuccessMessage(w) {
         $.each(w.data.results, function (index, item) {
         //logic to show the output in html page
                $('#itemsInDiv').append(item.Title + "---->" + item.FName);
            });

When the request has been processed and the data will be passed to success handler by SharePoint.


To get the clear exception, log the error details using data.responseText. Here I just renamed the list track to trac and logged the exception details.