Skip to main content

Cloud

Socialize Everything NewsGator Hack

Do you have something going on in SharePoint that you want to tie into NewsGator activity feed? In some cases an event receiver is what you want. See my blog on custom NG activities if you want to use an event receiver. But there are cases where SharePoint shamelessly writes things to its databases and doesn’t let you plug in with an event receiver. For example, the note board control uses JS to post to the SP database. To make such an event social (appear in the NG activity stream), you can do this JS hack: capture each add, edit, and delete event, and update the NG news stream accordingly.
The sample code below works with the out-of the-box note board web part. It does 3 things:

  1. For the add command, it simply captures the user input and posts to the NG community on the current site.
  2. For the edit event, it captures what the comment was before the user edited it, the same comment after, and replaces the comment. Downside is that if someone commented on the original comment, you’re out of luck; it’s deleted. This isn’t the perfect solution; you can add to it as needed.
  3. For he delete event, it captures the comment before it’s deleted form SharePoint and deletes it from the news stream.

NOTE: I hash-tagged the comment text when posting it to the activity feed. That happened to be appropriate for my use case, but also made it easier to extract the comment text for edit and delete commands.

   1: var oldCommentText;

   2: var newCommentText;

   3: 

   4: function socializePage() {

   5: 

   6:     // attach to all the buttons in the noteboard and just ignore when "Cancel" is hit. 

   7:     jQuery(':button', '.ms-socialCommentLoading').click(function () {

   8:         if (jQuery(this).text != "Cancel")

   9:             postToNG();

  10:     });

  11:

  12:     // attach to all the Delete and Edit links. They are going to be generated after the page loads, so you have to use .live()

  13:     jQuery('a', '.socialcomment-cmdlink').live("click", function () {

  14: 

  15:         if (jQuery(this).text() == "Delete") {

  16:             // climb up the table structure and then find commen text that is in the same table as the clicked link. 

  17:             var commentToDelete = jQuery('.socialcomment-contents', jQuery(this).parent().parent().parent().parent()).text()

  18:             deleteComment(commentToDelete);

  19:         } else {

  20:             oldCommentText = jQuery('.ms-socialCommentInputBox', '.socialcomment-edit').text(); // this is the edit box, it's 2nd on the page

  21:         }

  22:     });

  23: }

  24: 

  25: function postToNG() {

  26:     if (jQuery('.ms-socialCommentInputBox', '.ms-socialCommentEdit').text() != "") {

  27:         newCommentText = jQuery('.ms-socialCommentInputBox', '.ms-socialCommentEdit').text(); // this is the new comment box - first on the page

  28:     } else {

  29:         newCommentText = jQuery('.ms-socialCommentInputBox', '.socialcomment-edit').text(); // this is the edit box, it's 2nd on the page

  30:         if (oldCommentText != newCommentText) {

  31:             deleteComment(oldCommentText);

  32:         } else {

  33:             return false;

  34:         }

  35:     };

  36: 

  37:     // call to NG to post to the community that lives in the current site. Tag the comment with #PageComment to make it easier to find later

  38:     jQuery.ajax({

  39:         type: "POST",

  40:         contentType: "application/x-www-form-urlencoded",

  41:         url: "/_vti_bin/ng/activityfeed.svc/Context/",

  42:         data: newCommentText + " #PageComment " + window.location.href

  43:     });

  44:     return true;

  45: }

  46: 

  47: function deleteComment(commentText) {

  48:     var siteUrl = window.location.href.split("/Pages")[0];

  49: 

  50:     jQuery.ajax({

  51:         type: "GET",

  52:         contentType: "application/x-www-form-urlencoded",

  53:         url: siteUrl + "/_vti_bin/ng/activityfeed.svc/tag/pagecomment/", // get activity that's been tagged with #pagecomment

  54:         success: function (result) {

  55:             for (i = 4; i < result.firstChild.childNodes.length; i++)

  56:             {

  57:                 // extract the message text from the posts that were returned. We know it's going to be betweeen the @[community name] link and the # tag that we put at the end of the message earlier. Clever stuff...

  58:                 messageText = result.firstChild.childNodes[i].textContent.split("</a>:")[2];

  59:                 messageText = messageText.split('<span class="hashtag">#PageComment</span>')[0];

  60: 

  61:                 if (messageText.trim() == commentText.trim()) {

  62:                     // get event key out of the retrned data. You need it to delete

  63:                     eventKey = result.firstChild.childNodes[i].childNodes[12].childNodes[0].data;

  64: 

  65:                     jQuery.ajax({

  66:                         type: "DELETE",

  67:                         contentType: "application/x-www-form-urlencoded",

  68:                         url: "/_vti_bin/ng/activityfeed.svc/" + eventKey,

  69:                         //success: function (result) {

  70:                         //    alert("deletion success! ");

  71:                         //},

  72:                         error: function (result) {

  73:                             alert("fail:" + result.statusText);

  74:                         }

  75:                     });

  76:                 }

  77:             }

  78: 

  79:         },

  80:         error: function (result) {

  81:             alert("fail:" + result.statusText);

  82:         }

  83:     });

  84: }

I used Fiddler to tune the queries; if you need to customize my solution, I highly recommend testing your API calls in fiddler first.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Darya Orlova

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram