-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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