locked
Getting the number of comments on a page with JavaScript (SocialCommentControl) RRS feed

  • Question

  • Hi, guys
    I have a small and well-defined problem, but it's bugging me nevertheless. In the page layout for our news page, we have a SocialCommentControl like this:

    <SharePointPortalControls:SocialCommentControl runat="server"/>

    Users can read, add, edit and delete comments without problems. Works like a charm.

    I need to write JavaScript code to present the number of comments for the current page in a div element.
    I've found a lot of server side examples (C#) on this, but haven't been able to translate it to JSOM.

    Can someone please give me a hint?

    Thanks.




    • Edited by Papageno Sweden Monday, November 9, 2015 8:36 AM Removed a confusing code snippet
    Friday, November 6, 2015 5:00 PM

Answers

All replies

  • Hi,

    We can get social comments count using SharePoint web service('/vti_bin/SocialDataService.asmxSocialDataService.asmx?op=CountCommentsOnUrl') and jQuery Ajax to achieve it. The following blogs for your reference:

    http://www.c-sharpcorner.com/Blogs/46386/getting-comment-count-based-on-url.aspx

    https://mohitvash.wordpress.com/2014/05/22/get-comment-count-in-sharepoint/

    http://mithunkarthik.blogspot.com/2010/12/sharepoint-2010-how-to-get-social.html

    Best Regards,

    Dennis Guo


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.


    Monday, November 9, 2015 1:41 AM
  • Thank you, Dennis, for solving this!

    I was expecting some JSOM, but tried your suggestion and it worked fine. The three blog posts mentioned above pretty much explains the same. I found the one from c-sharpcorner the easiest to read and any new readers can find a tweaked version in the code below. I've also removed my sample code above, since it didn't work and would just confuse people.

    If anyone else faces the same issue of getting the number of social comments, this is the way to go:

    getCommentsCount(window.location.href); // Calling the function below sending the current page url
    
    function getCommentsCount(url) {
      var dataXML = GenerateDataXML([url]);
      $.ajax({
        url: "/_vti_bin/SocialDataService.asmx?op=CountCommentsOnUrl",
        data: dataXML,
        type: "POST",
        async: true,
        complete: processResult,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml"
      })
    
      function GenerateDataXML (url) {
        var soapXml = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
        "<soap:Body>" +
        "<CountCommentsOnUrl xmlns=\"http://microsoft.com/webservices/SharePointPortalServer/SocialDataService\">" +
        " <url>" + url + "</url>" +
        "</CountCommentsOnUrl>" +
        "</soap:Body>" +
        "</soap:Envelope>"
        return soapXml;
      }
    
      function processResult(content, txtFunc, xhr) {
        var numberOfComments = $(content.responseText).find('CountCommentsOnUrlResult').text();
        var commentsmessage = (numberOfComments == 1) ? " comment" : " comments";
        $("#comments").html(numberOfComments + commentsmessage);
      }
    }


    Monday, November 9, 2015 8:34 AM