Posts

Showing posts from April, 2021

How to make Rest API call synchronous in SharePoint

Image
How to make Rest API call synchronous in SharePoint In this post, I will discuss how we can make a synchronous Rest API call in SharePoint using jQuery. Normally when we do Rest API call using AJAX calls in SharePoint, it executed asynchronously. So it is not possible to know whether the call has been made completely or not. Sometimes you might come across a requirement where you want to execute something after the call has been made complete. Here I will show some ways of make synchronous Rest API call in SharePoint - 1. Using then - getItems().then(getItemsSuccess, getItemsFail); function getItems(){ return $.ajax({ url:  _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('ListName')/items", type: "GET", headers: { "accept": "application/json;odata=verbose", } }); } function getItemsSuccess(data){ if(data.d.results.length > 0){   //do something here // } } function getItemsFail(err){   alert(&quo

How to use Deferred when done in JSOM SharePoint

Image
In this post we will see how we can use Deferred with when in JavaScript Object Model to make a synchronous call in SharePoint. As in many of the requirements we need to make asynchronous call to synchronous for easy of work. In the below example we will update multiple SharePoint list items one by one. Code -  var itemIDs = [1,2,3]; $(window).load(function() {      $("#btnSubmit").click(function(){                        $.each(itemIDs, function(index, value){                           $.when(updateListItem(value))        .done(function (data) {                   alert("All Items Updated);                    })        .fail(function (sender, args) {           alert('Failed');     });             });            });   });  var dfd = $.Deferred(); function updateListItem(value) {     var clientContext = new SP.ClientContext.get_current();     var oList = clientContext.get_web().get_lists().getByTitle('Advisory Panel');     this.oList