This SharePoint Rest API tutorial, we will discuss, how to use deferred promise and then in Rest API in SharePoint Online or SharePoint 2013/2016 to make a synchronous call.
We can use Deferred promise and then in Rest API in SharePoint 2013/2016/Online to make calls. Let us consider an example and then we will see how we can implement this.
Here let us take a list which has three columns:
- Title
- FirstName
- LastName
And let us create an HTML form where the user can enter a title, first name, and last name and click on the submit button to save the data in the list and once the data get saved it should display in a div. So in this situation, we need a synchronous call.
Our Save to list code should execute first and once it is complete then our Retrieve list items code should execute which will fetch the data from list and show in a div.
SharePoint rest api synchronous call
We can use Deferred promise and then to make SharePoint rest api synchronous call.
Syntax:
Here we can declare Deferred like below
var deferred = $.Deferred();
And then once method() execute completely method2() will get called.
method1().then(function(data) {
method2();
});
});
In the first method (method1()), we have to return promise() like below:
var method1 = function () {
return deferred.promise();
}
Then we can write the success and failure method like below:
function onQuerySucceeded(sender, args) {
deferred.resolve(args);
}
function onQueryFailed() {
deferred.reject(sender, args);
}
Full Code:
<div id="AddListData">
<div>
Title:
<br />
<input type="text" id="txtTitle" />
</div>
<div>
Industry:
<br />
<input type="text" id="txtIndustry" />
</div>
<br />
<div>
<input id="btnSubmit" type="button" value="Submit" />
</div>
</div>
<div id="divResult"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var deferred = $.Deferred();
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
addListItem().then(function (data) {
getListData();
});
});
}
var addListItem = function () {
var title = $("#txtTitle").val();
var industry = $("#txtIndustry").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyCompanyInfo')/items";
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.MyCompanyInfoListItem' },
'Title': title,
'Industry': industry
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
return deferred.promise();
}
function onQuerySucceeded(sender, args) {
deferred.resolve(args);
}
function onQueryFailed() {
deferred.reject(sender, args);
}
function getListData() {
var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyCompanyInfo')/items";
$.ajax({
url: fullUrl,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: onQuerySucceeded1,
error: onQueryFailed1
});
}
function onQuerySucceeded1(data) {
var listItemInfo = ";
$.each(data.d.results, function (key, value) {
listItemInfo += '<b>Title:</b> ' + value.Title + ' – <b>Industry:</b> ' + value.Industry + '<br />';
});
$("#divResult").html(listItemInfo);
}
function onQueryFailed1() {
alert('Error!');
}
</script>
Once you save the code, the form will appear like below. User can enter a value and click on submit, it will first save the data to the SharePoint list and then display the result in the page.
You may like following SharePoint Rest API tutorials:
- Display SharePoint List items in a data table using Rest API and jQuery
- Create custom SharePoint Survey poll using REST API and AngularJS
- Retrieve and Display TASK status using REST API in SharePoint
- Display Task List data in a table using SharePoint REST API and filter by status column
- Update People Picker Group field using Rest API and jQuery in SharePoint 2013/2016
- KnockoutJS SharePoint 2013 CRUD Operations using REST API
- SharePoint 2013 Add Quick Launch or left navigation link in Site Using REST API
- Delete all items from SharePoint Online list using Rest API
- Create and delete a file using Rest API in SharePoint Online/2013/2016
- Create, Update and Delete SharePoint List Item using Rest API
- Insert and Retrieve items to SharePoint Online list using Rest API
- Currency Converter in SharePoint Online using JavaScript and REST API
This is how we can make rest api synchronous call in SharePoint Online or SharePoint 2013/2016.
I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com
[…] to use Deferred promise and then in Rest API in SharePoint Online or SharePoint […]
[…] to use Deferred promise and then in Rest API in SharePoint Online or SharePoint […]