Quantcast
Channel: SharePoint Developer Support Team Blog
Viewing all articles
Browse latest Browse all 100

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

$
0
0

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.

Alerts

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;
}
else
{
     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;
 
alert.Update();

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

Happy Coding! Smile


Viewing all articles
Browse latest Browse all 100

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>