I want to create one common jquery library to work with REST queries in sharepoint. I have two files.
- REST_Operations.js
- consumer.js
In REST_Operations.js I am writing rest queries with deferred object as follows.
"use strict";
var RESTQueries = RESTQueries || {};
RESTQueries.getItemsFromList = function(){
var deferred = $.Deferred();
var execute = function(){
$.ajax({
url:"https://mysiteurl/sites/practice/_api/web/lists/getByTitle('Pages')/items",
method:'GET',
headers:{Accept:'application/json; odata=verbose'},
success:function(data){
deferred.resolve(data);
},
error:function(err){
deferred.reject(err);
}
});
return deferred;
};
return {
execute:execute
}
}();
RESTQueries.readItemsFromList = function(){
RESTQueries.getItemsFromList.execute().promise().then(function(data){
return data;
});
}
If i put console.log in then()
function, data is coming properly.
I have one more js file, cosumer.js
, in that i am calling readItemsFromList
as follows
var items = RESTQueries.readItemsFromList();
so my idea is items
variable should contain the json object or result for the rest query.
But if i put console.log(items)
, output is coming as undefined
.
How to get the json object in the items
variable?
If I try with following code
RESTQueries.readItemsFromList = function(siteUrl,listName){
return (RESTQueries.getItemsFromList.execute(siteUrl,listName).promise().then(function(data){
console.log(data);
return data;
}));
}
it is giving me different kind of object. Find the below screenshot.