Skip to content

Instantly share code, notes, and snippets.

@lsuarezj
Forked from bruzie/getBlogItems.js
Created December 13, 2017 21:31
Show Gist options
  • Select an option

  • Save lsuarezj/d3c32b0f935ca492df8a1a07aa843a2b to your computer and use it in GitHub Desktop.

Select an option

Save lsuarezj/d3c32b0f935ca492df8a1a07aa843a2b to your computer and use it in GitHub Desktop.
SharePoint 2013 REST: get number of comments on a post (a special kind of Lookup field)
// Getting list items based on ODATA Query
function getListItems(url, listname, query, complete, failure) {
// Executing our items via an ajax request
$.ajax({
url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
complete(data); // Returns JSON collection of the results
},
error: function (data) {
failure(data);
}
});
}
function getNews() {
// The 'Id' property of the NumComments field holds the comment count. It is a lookup to the Comments list, using the PostTitle field,
// but it is actually an aggregate
var query = '?$select=ID,Title,PublishedDate,PostCategory/Id,PostCategory/Title,LikesCount,NumComments/Id&$expand=PostCategory,NumComments&$orderby=PublishedDate%20desc&$top=3';
getListItems(_spPageContextInfo.webAbsoluteUrl+'/news','Posts',query,getNewsSuccess,getListItemsError);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment