Hi All,
In one of our news listing web part we have requirement to show the comments count for given news. On news pages we are using the NoteBoard web part for commenting purpose.
For news listing we are using OOB Content by Search (CBS) web part and in front of news title we need to show the comment count.
Since we are using OOB web part we don’t have an option of code where we can create SocialCommentManager class and call GetCount(Uri) method. The other option is to use the SocialDataService (SocialDataService.asmx) and call this from the display template of CBS web part. This solution is also compatible with Office 365.
Following is the sample script:
- function ReturnCommentsCount(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 commentsCount = $(content.responseText).find('CountCommentsOnUrlResult').text();
- }
Finally from display template we can directly call ReturnCommentsCount(news url) and set the comment count to HTML element in front of news title like for example consider <span> element having id "commentcount" so like:
- $("#commentcount”).text(commentsCount);
Complete processResult method will be like:
- function processResult(content, txtFunc, xhr) {
-
-
- var commentsCount = $(content.responseText).find('CountCommentsOnUrlResult').text();
- $("#commentcount”).text(commentsCount);
- }
Thanks!
Enjoy reading
Feel free if any comment / question.