This post is a contribution from Jaishree Thiyagarajan, an engineer with the SharePoint Developer Support team.
Let’s first talk about some basic facts about discussion board and alert terminologies.
We all know about discussion board. Discussion board is where we can add “posts” and for every single post we can add “replies (comments)”. The discussion board is designed in such a way that all posts are stored as Folders and replies to the posts are stored as items within the folder.
In SharePoint, we can subscribe for alerts at the list/library levels and also at individual item levels.
Now that we know that “posts” are added as folder and “replies” are added as items to the folder in the discussion board , the first question that pops in our mind is “Which alert-type (SPAlertType) should I use?”
If I use SPAlertType.Item for a post then I would not get alert for newly added replies. If I use SPAlertType.List then I may end up receiving alerts for items that I didn’t subscribe to.
If you are looking for a solution to receive the alerts for a “post you are subscribed” and “any replies added to the subscribed post”, then you are at the right blog post.
Now, let’s check out the alert terminology for better understanding.
In SharePoint, there are two types of alerts:
- Subscription alert: Whenever a user subscribes for an item/document/list, the user will get an email stating “you have subscribed for an alert…”.
- Changed alert (commonly referred to as alert): Based on the subscription option chosen (added, modified etc.,) alert email will be triggered.
As we can see, any site administrator can subscribe any user for an alert. These subscription alerts are NOT security trimmed. But the changed alerts are security trimmed. The alert emails will only be triggered if the subscribed user has sufficient permissions for the item (for item level alerts) or lists (for list level alerts).
Now, if your users complain that they are not receiving the changed alerts, your first action should be to check whether they have sufficient permissions for subscribed item or not.
Ok – we’ve learnt some details on discussion board and alerts. Now, let’s check out how to subscribe for a Post in discussion board programmatically.
Subscribe for a “Post” in discussion board programmatically
Posts are added as items (i.e., in UI when you open a discussion board, you can see all the posts listed as items; the folder structure mentioned above is the internal design) to a discussion board and so whenever we subscribe for a post, item level alerts would be added via UI. Assuming this, if we specify SPAlertType.Item when programmatically subscribing to an item in discussion board, alerts would not be sent out for replies to the post.
To circumvent this, if we set SPAlertType.List, alerts would be sent out for any new post to the discussion board, but the requirement is to receive alert emails for just a particular post and replies to that post.
The trick to achieve this requirement programmatically is to specify alert filter in the code.
The above code will work for discussion board present in the root site; but for sub-sites it won’t work.
After breaking my head for a long time and comparing programmatically added subscription with the out of the box subscription (in the ImmediateSubscription table in SharePoint content database, thanks to my colleague Selvakumar Ganapathi, escalation resource from SharePoint team), I finally figured out the value passed to “ItemFullUrl” should begin with a “/” (forward slash).
Here’s the complete and working code for subscribing an alert for a post in a discussion board
The above code works for alerts subscribed to a discussion board post at the root as well as sub-sites.