Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillipharding/deabdec1d1bef705bcbc to your computer and use it in GitHub Desktop.
Save phillipharding/deabdec1d1bef705bcbc to your computer and use it in GitHub Desktop.
news.popular.getPosts = function() {
var
p = new $.Deferred(),
r = {
url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Posts')/items?$select=Id,Title,PublishedDate,LikesCount,NumCommentsId,PostCategoryId&$orderby=PublishedDate desc"
+ getFilter(),
type: 'GET',
headers: { ACCEPT: 'application/json;odata=minimalmetadata' }
};
$.ajax(r)
.done(function(response) {
var data = (response.value || response.d.results || response.d);
var populardata = [];
$.each(data, function(i,e) {
if (e.LikesCount || e.NumCommentsId) {
populardata.push(e);
}
});
p.resolve(populardata);
})
.fail(function (xhrObj, textStatus, err) {
var e = JSON.parse(xhrObj.responseText),
err = e.error || e["odata.error"],
m = '<div style="color:red;font-size:1.2em;">Exception<br/>&raquo; ' +
((err && err.message && err.message.value) ? err.message.value : (xhrObj.status + ' ' + xhrObj.statusText))
+'</div>';
p.reject({ success: false, error: m, uri: r.url });
});
return p.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment