Hi again.
Today, I'll explain a requirement and approach that we finalized. I am sharing this because I feel this is really good (however a little 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 a custom page layout for news pages. Thousands of news are already created. So this change is required on new as well as on existing news pages.
On a News page we have a NoteBoard web part for posting comments as in the following:
Figure 1: Note Board web part
Challenge: Here we have the challenge that we couldn't bind an event to the “Post” button of the NoteBoard web part since it is called asynchronously so that we could write some JavaScript function and send email to the news publisher (news creator).
So here we don't have any event to use.
Solution / Approach: Here, we did slightly change the requirements since there doesn't seem to be any option. Rather than sending mail on every comment post, we planned to send mail on a page refresh if new comments are added to the page.
So the high-level approach is the following:
- Add one number field to the News content type to hold the number of comments on the current page.
- On existing news pages that are already created, the default value will be the current comment count. New news pages that will be created on them has a default value of zero (0).
- Write JSOM code (new JavaScript file) and use content editor / script editor web part to link the JavaScript file and add a web part to the news page layout. On existing news pages use a PowerShell script to add a web part.
- The following code must be written in the preceding JavaScript file.
- Read the current page comment count (we can use SocialDataService.asmx and the operation CountCommentsOnUrl). An added respective blog URL in the reference list.
- Compare the preceding comment count with the value in our field on the page.
- If the current comment count is greater than (or less than) the page field value, this means new comments are added (or removed) on the news page and mail will be sent to the news publisher.
- Update the page field with the current comment count.
- New Challenge: But here again there is the challenge of page versioning, since each time a new comment is added, the page field is updated and the version of the page will be increased.
- Solution: Use the SystemUpdate(false) method, but this doesn't work from JSOM. JSOM doesn't support SystemUpdate(). So again here the option is to write the service (WCF), that will be hosted on WFE and write code to update the page using SystemUpdate().
- Problem with this approach
Mail will not be sent immediately once the comment is posted on the news page.
We don't have the option to alert the publisher if a user edits his comments. So unless and until new comments are not added (deleted) and the news page is not refreshed again, the news publisher won’t have an alert regarding comment editing.
References
- Adding a web part on the page using a PowerShell script: Prasham's SharePoint Blogs.
- Getting the current page comments count: Getting Comment Count Based on URL.
Thanks for reading.
Any suggestions / comments / feedback are most welcome.