how can I get my posts count that I have posted in the discussion board using JSOM or REST API and show the count in HTML div tag


You can get the posts count in REST API using data.d.results.length. Then append the count to the div using jQuery as shown below:

var currentUserId;
function getItems(url) {  
currentUserId = _spPageContextInfo.userId;
        return $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + url,
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",

getItems( "/_api/Web/Lists/GetByTitle('<list title>')/Items?$select=Author/Id,*&$expand=Author&$filter=(Author/Id eq '+currentUserId+')").done(function(data){

    var numofItems = data.d.results.length;
     $("#<div id>").append("<b>" + numofItems + "</b>");
  • 1
    Karen is asking about count of his posts only. So URL should be specific to his user ID. – Ganesh Sanap Sep 23 '18 at 16:59
  • REST URL updated to filter items based on the author. – Sohail Shaikh Sep 23 '18 at 17:39
  • 1
    "_spPageContextInfo.userId" gets the current login user id that i have stored in variable "currentUserId" – Sohail Shaikh Sep 23 '18 at 18:21
  • it is working now but it always returns 0 – Karen Kumar Sep 23 '18 at 18:33
  • please help @sohail – Karen Kumar Sep 23 '18 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.