Getting the Reply Count for a SharePoint Discussion Using the REST API

The other day I was creating a custom UI for a SharePoint 2013 Discussion list. To me, the out-of-the-box UI in SharePoint 2013 is definitely a step forward from previous versions. It’s still pretty rudimentary, though, and my client wanted something “more like other forums”.

SharePoint’s Discussion lists are sort of like Document Sets, in that the original post is a Discussion Content Type which inherits from Folder and the replies are the Message Content Type, which inherits from Item. So there aren’t any Documents involved, but Discussions are once again glorified Folders.

Since I’m using KnockoutJS on this project, I can make the UI look like pretty much anything. One thing we wanted to display was a reply count per thread. This seemed easy enough, but it could get expensive to retrieve all of the replies just to count them for the UI. Unfortunately, there was no obvious column (like “Replies” maybe?) to give me the answer.

Trolling around SharePoint StackExchange, I ran across a post by my friend Rothrock entitled How to get ItemChildCount of DocumentSet (folder) using REST Api asking a pretty similar question, but in his case it was about Document Sets. There ought to be a field like ItemChildCount or something, but he couldn’t find it, either. We’re used to a field like that using SPServices and SOAP, but there didn’t seem to be anything analogous in REST.

Luckily, someone who goes by ECM4D answered Rothrock with this example. (There were typos, but this was the idea.)

/_api/web/lists/getbytitle('your_list')/items?
  $select=ID,Title,Folder/ItemCount
  &$expand=Folder/ItemCount
  &$filter=FSObjType eq 1

By expanding the Folder, we can get at an ItemCount easily, just as we’d like to expect. This is yet another example where the documentation for the REST services simple doesn’t go deep enough to help us. Because much of the documentation is example-based – and this isn’t in any of the examples – we’re out of luck.

I ended up with something pretty similar, and it works great. Note that the ItemCount includes *all* replies, which means replies to the original post as well as replies to replies. If you wanted just replies to the original post, you’d need some other method.

MyProject.Promises.Discussions = $.ajax({
  url: _spPageContextInfo.webAbsoluteUrl +
    "/_api/web/lists/getbytitle('Discussion')/items?" +
    "$select=ID,Title,FileRef,IsFeatured,Created,Author/Title,Folder/ItemCount" +
    "&$expand=Author,Folder",
  method: "GET",
  headers: {
    "Accept": "application/json; odata=verbose"
  }
});

This call – using jQuery’s $.ajax function – gets me the basic info about the Discussion items I need:

  • ID and FileRef let me provide links for the user to use to go deeper into the threads
  • Title is the Subject of the original post
  • IsFeatured tells me if it’s a featured post so that I can highlight it in some way
  • Created and Author tell me who started the thread and when
  • Folder/ItemCount is that mysterious count of replies I was looking for

All pretty easy, really, once I found that thread on SharePoint StackExchange. One of the best ways to learn the ins and out of the REST endpoints is to troll those public forums. Be forewarned, though: you’re just as likely to find something that doesn’t work as something that does.

6 Comments

  1. Thanks for this! it was driving me insane. I do have one question ………….. at the moment my code links to the dispform.aspx with the ID. This only shows the original message, not the message and replies. I know i need to link to the flat.aspx file but what ID do i pass from the rest call above in your code?

    Reply
    • @novak84:

      The URL for the original post with all the replies is actually a link to the folder which contains the post. The URL will look something like this:
      /sites/MySite/Lists/Discussion/Flat.aspx?RootFolder=%2Fsites%2FMySite%2FLists%2FDiscussion%2FSubject%20of%20the%20post&FolderCTID=0x01200200991E31338CA2234191BCDC9E651B605D
      The important bits of this URL should be available to you in the ServerRelativeUrl property of the results.

      M.

      Reply
  2. Hi Marc, I am currently working on a project involving a SharePoint site discussion list, I need to basically re do all the UI and make it look something similar to a Facebook posts with comments. Searching for something to point me in the right direction I found John Liu blog where he describes how to create discussions and replies with SPServices:

    http://johnliu.net/blog/2014/1/26/using-spservices-to-create-discussion-and-reply-in-a-discuss.html

    His approach is rather simplistic from a UI standpoint, it does not comment on how to achieve functionality such as async refresh when submitting a reply, scroll paging (load new elements as the user scrolls), image attachment. Oh boy sounds like I’m asking for the hole solution.

    Reply
    • @Fabian:

      Having tried to wrestle discussion lists into submission in the past, I can tell you you’ll have a lot of work here. The types of things you want to do are very specific to your own requirements, so you’re not going to find exactly that anywhere. Time to roll up the sleeves!

      M.

      Reply

Have a thought or opinion?