SharePoint Developer Support Team Blog

The Official blog of the SharePoint Developer Support Team

How to subscribe for an alert for a “Post” in discussion board programmatically

How to subscribe for an alert for a “Post” in discussion board programmatically

  • Comments 6
  • Likes

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.

Discussion Board

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:

  1. Subscription alert: Whenever a user subscribes for an item/document/list, the user will get an email stating “you have subscribed for an alert…”.
  2. 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.

SPList list = web.Lists["Team Discussion"];
SPListItem item = list.GetItemById(8);
SPUser user = web.AllUsers[@"mydomain\username"];
SPAlert alert = user.Alerts.Add();
alert.AlertType = SPAlertType.List;
alert.AlertTemplate = list.AlertTemplate;
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.EventType = SPEventType.All;
// Here, we filter the alert for the specific discussion, so only responses to that discussion get an alert
alert.Filter = string.Format("<Query><BeginsWith><FieldRef Name=\"ItemFullUrl\"/><Value type=\"string\">{0}</Value></BeginsWith></Query>", item.Url);
alert.List = list;
alert.Title = "Team Discussion: Sports";

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
string url = "";
if (item.ParentList.ParentWebUrl == "/")
     url = item.Url;
     url = item.ParentList.ParentWebUrl + "/" + item.Url;
string itemFullUrl = url.StartsWith("/") ? url.Substring(1) : url;
string itemBeginswithFilter = itemFullUrl + "/";
string filter = string.Format("<Query><Or><Eq><FieldRef Name=\"ItemFullUrl\"/><Value type=\"string\">{0}</Value></Eq><BeginsWith><FieldRef Name=\"ItemFullUrl\"/><Value type=\"string\">{1}</Value></BeginsWith></Or></Query>", itemFullUrl, itemBeginswithFilter);
SPList list = web.Lists["Sports"];//list name
SPListItem item = list.GetItemById(1);//post ID
SPUser user = web.AllUsers["mydomain\\username"];//user
SPAlert alert = user.Alerts.Add();
alert.AlertType = SPAlertType.List;
alert.AlertTemplate = list.AlertTemplate;
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.EventType = SPEventType.Add;
alert.Filter = filter;
alert.Title = "Sports: Cricket"; //title
alert.List = list;

The above code works for alerts subscribed to a discussion board post at the root as well as sub-sites.

Happy Coding! Smile

  • Wonderful article to understand the alert basics for dicussion board. Thank you Jaishree for posting this.

  • Nice article.

  • For this requirement do i need to create a custom column as "ItemfullUrl" with value discussion folder item relative url?



  • I like your use of discussion board for sharing work for feedback.

  • A licensed Loan Lender, We offer Affordable Loan at 3% interest rate available for local and international borrowers, Are you seriously interested in getting a genuine Loan without stress? Do you need this Loan for business and to clear your bills? Then send us an email now for more details via:

  • 24 Hours Loan Offer At 2%

    Do you need an urgent loan as fast as 24 Hours? if Yes Etihad Loans Limited Offer all kinds of loan to individuals and companies in need of loan at low interest rate of 2% . We offer loan ranging from (5,000 $,€ & £ ) to (100,000,000 $,€ & £ )if you need a loan today Contact us Via Email { } for a loan:


    Monthly income:
    Loan Amount:
    Purpose of Loan.
    Phone Number:


    Etihad Loans Limited

    Make Sure you send your Reply to :

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment