Hide / unhide the CommentBox, display number of custom comments in the homepage

Home Forums Comment box for SharePoint Hide / unhide the CommentBox, display number of custom comments in the homepage

Viewing 6 reply threads
  • Author
    Posts
    • #10578
      William
      Participant

        Dear Alexander,

        Is it possible to add a button to hide and unhide the CommentBox?
        I would also like to know if it is possible to customize the display of number of comments (SPJS-Comments instead of OOTB Comments).

        Thank you !

      • #10621
        Alexander Bautz
        Keymaster

          Currently all parts of the comment box instance is not contained in an outer “wrap” so you cannot toggle it easily. I’ll look at this in the next version.

          I’m not sure I understand what you mean by customizing the comment count display – can you add a picture as an attachment?

          Alexander

        • #10699
          William
          Participant

            Dear Alexander,

            Sorry for my late reply, I meant this link displaying the number of comments (image attached).

            Thank you,

            Attachments:
          • #10744
            Alexander Bautz
            Keymaster

              Hi,
              You can put this code at the bottom of the page from your screenshot above:

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
              <script type="text/javascript">
              jQuery(".ms-comm-metalineItemSeparator").each(function(i,a){
              	var b = _spPageContextInfo.webServerRelativeUrl+a.href.split(_spPageContextInfo.webAbsoluteUrl)[1].split("#")[0];
              	$.ajax({
              		type:"GET",
              		url:_spPageContextInfo.webServerRelativeUrl+"/_api/web/lists/getByTitle('SPJS-CommentBox')/items?$filter=substringof('"+b+"',Title)",
              		method: "GET",
              		headers: {"Accept": "application/json; odata=verbose"},
              		success:function(data){
              			$(a).text(data.d.results.length+" Comments")
              		},
              		error:function(data){
              		   //console.log(data);
              		} 
              	});
              });
              </script>

              Let me know if you have any questions.

              Alexander

              • This reply was modified 8 years, 9 months ago by Alexander Bautz. Reason: Changed POST to GET
              • This reply was modified 8 years, 9 months ago by Alexander Bautz.
            • #10784
              William
              Participant

                Dear Alexander,

                I have added a CEWP in my Blog homepage and loaded the script you provided, but the number of comments still shows 0.
                Can you provide more guidance, please?

                Thanks for your work,

                William

              • #10805
                Alexander Bautz
                Keymaster

                  Hi,
                  It looks like the code must be loaded with a slight delay to allow for the original comment count function to finish drawing the part we need to change. Try changing it like this:

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
                  <script type="text/javascript">
                  function spjsCommentCount(){
                  	jQuery(".ms-comm-metalineItemSeparator").each(function(i,a){
                  		var b = _spPageContextInfo.webServerRelativeUrl+a.href.split(_spPageContextInfo.webAbsoluteUrl)[1].split("#")[0];
                  		$.ajax({
                  			type:"GET",
                  			url:_spPageContextInfo.webServerRelativeUrl+"/_api/web/lists/getByTitle('SPJS-CommentBox')/items?$filter=substringof('"+b+"',Title)",
                  			method: "GET",
                  			headers: {"Accept": "application/json; odata=verbose"},
                  			success:function(data){
                  				$(a).text(data.d.results.length+" Comments")
                  			},
                  			error:function(data){
                  			   //console.log(data);
                  			} 
                  		});
                  	});
                  }
                  _spBodyOnLoadFunctionNames.push("spjsCommentCount");
                  </script>

                  Please note that you should not load jQuery more than one time in a page. If you already have this laded – for example in the master page – skip the line form this code example.

                  Alexander

                • #10831
                  William
                  Participant

                    Dear Alexander,

                    Thank you, it works perfectly !!

                Viewing 6 reply threads
                • You must be logged in to reply to this topic.