Wednesday, 6 May 2015

Sending email when comment posted using NoteBoard web part on News page (publishing page)

Hi again,

Today, I’ll discuss regarding one requirement and approach which we finalized. I am sharing this because I feel this is really good (little bit challenging) requirement and then also want to validate our solution / approach. I would definitely like to receive feedback / comments / or other better approaches if possible.

Requirement: Sending email alerts when user post comments on news pages

Background: We have our publishing portal. We have custom page layout for news pages. Thousands of news already created. So this change required on new as well as on existing news pages.

On News page we have NoteBoard web part for posting comments as

Fig1: NoteBoard web part

Challenge:

Here we have challenge, we couldn’t bind event to “Post” button of NoteBoard web part since it calls asynchronously so that we could write some JavaScript function and send email to news publisher (news creator).

So here we don’t have any event to use.

Solution / Approach:

Here, we slightly did change in requirement since doesn’t seems to be any option. Rather than sending mail on every comment post, we planned to send mail on page refresh if new comments are added on the page.

So high level approach is
  1. Add one number field to News content type to hold the no of comments on current page

  2. On existing news pages which are already created, default value will be the current comment count. New news pages which will be created on them default value will be zero (0)

  3. Write JSOM code (new JS file) and use content editor / script editor web part to link JS file, add web part to news page layout. On existing news pages use PowerShell script to add web part

  4. Following code need write in above JS file
    • Read the current page comment count (we can use SocialDataService.asmx and operation - CountCommentsOnUrl). Added respective blog URL in reference list

    • Compare the above comment count with value in our field on the page
    • If current comment count is greater than (or less than) page field value, this means new comments are added (or removed) on the news page, mail will be sent to news publisher

    • Update the page field with the current comment count
      • New Challenge: But here again there is challenge of page versioning, since each time new comment will be added, page field is updated, version of the page will be increased

      • Solution: Use SystemUpdate(false) method but this doesn’t work from JSOM. JSOM doesn’t support the SystemUpdate(). So again here option is to write the service (WCF), which will be hosted on WFE and write code update the page with SystemUpdate()

  5. Problem with this approach:
    • Mail will not be sent immediately once the comment is posted on news page

    • Don’t have option to alert the publisher if any user edit his comments. So unless and until new comments is not added (deleted) and news page is not refreshed again the news publisher won’t have alert regarding comment editing

References:
  1. Adding web part on the page using PowerShell script: http://prashamsabadra.blogspot.in/2012/11/adding-web-part-on-pages-using.html
  2. Getting current page comments count:
    http://www.c-sharpcorner.com/Blogs/46386/getting-comment-count-based-on-url.aspx

Thanks!

Enjoy SharePoint :)

Any suggestions / comments / feedback are most welcome :)

No comments: