January 15, 2009

How to create SharePoint alerts for Discussion Board list items programmatically

First, thank you to Pam for posting a good remark about my previous article: "How to create SharePoint alerts for lists or list items programmatically"

In fact, she has noticed in using my code to create an alert for a Discussion Board list item that the values stored in the ImmedSubscriptions table in the content db were not the same than an alert created from the user interface. According to the values stored in the db, an alert created from the user interface seems to be like an alert created at the list level with a filter referencing the item.

To understand why an alert for a Discussion Board list item seems to be like an alert created at the list level with a filter, we need to create an alert for a Discussion Board list item and an alert for e.g. a Documents Library list item from the user interface and to compare themselves together.

In doing that, we can see the difference between the two alerts. In the case of an alert for Discussion Board list item, the screen is exactly the same than the screen displayed for a list (more options than for a list item) and the values stored in the db show that it is like an alert for a list.





DB Values
ListId: 57AD9100-E9B8-4097-9CC3-97F87FC50CA8
ListUrl: Shared Documents
ListTitle: Shared Documents
ItemId: 1
AlertTitle: Shared Documents: WSS_Quick_Reference_Guide.doc
AlertType: 1
AlertTemplateName: SPAlertTemplateType.DocumentLibrary
EventType: -1
Filter:

In the case of an alert for Documents Library list item, the screen is the screen usually displayed for a list item and the values stored in the db show that it is an alert for a list item.





DB Values
ListId: 3F8B0E63-2005-4F5F-AF93-FDF20D54AB1A
ListUrl: Lists/Team Discussion
ListTitle: Team Discussion
ItemId: NULL
AlertTitle: Team Discussion: test
AlertType: 0
AlertTemplateName: SPAlertTemplateType.DiscussionBoard
EventType: -1
Filter: lists/team discussion/test

After a good reflection, it's logical. When you create an alert on e.g. a Documents Library list item, the alert is created at the list item level (e.g. someone changes metadata, etc...). In the case of a Discussion Board list item, when someone posts a reply, it doesn't change the list item itself. In fact, it creates a new list item linked with it. That explains why the values stored seems like to be values for a list level alert except the filter referencing the item.


In this way, the code to create an alert for a Discussion Board list item is exactly the same than the one used for a an alert for list with the filter referencing the item by its url.


using (SPSite mySiteCollection = new SPSite("http://mossjno/
"
))
{
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
    {
        string myUserLogin = @"MOSSJNO\tester1";
        SPUser myUser = mySite.AllUsers[myUserLogin];
        SPAlert newAlert = myUser.Alerts.Add();
        newAlert.Title = "Test alert for Discussion Board list item";
 
        SPList myList = mySite.Lists["Team Discussion"];
        SPListItem myListItem = myList.GetItemById(1);
        newAlert.AlertType = SPAlertType.List;
        newAlert.List = myList;
        newAlert.AlertTemplate = myList.AlertTemplate;
 
        newAlert.EventType = SPEventType.All;
 
        newAlert.Filter = string.Format("<Query><BeginsWith><FieldRef Name=\"ItemFullUrl\"/><Value type=\"string\">{0}</Value></BeginsWith></Query>", myListItem.Url);
        newAlert.AlertFrequency = SPAlertFrequency.Immediate;
 
        newAlert.Update(true);
    }
}

DB Values
ListId: 3F8B0E63-2005-4F5F-AF93-FDF20D54AB1A
ListUrl: Lists/Team Discussion
ListTitle: Team Discussion
ItemId: NULL
AlertTitle: Test alert for Discussion Board list item
AlertType: 0
AlertTemplateName: SPAlertTemplateType.DiscussionBoard
EventType: -1
Filter: lists/team discussion/test

3 comments:

Anonymous said...

Hi Jérôme,

I really, really appreciate your article on this Discussion Board Alert topic.

In my opinion it is the best that I could find on the internet, when I was stuck in that really confusing SPAlert world ;-)

But anyway, I ran into trouble again with that topic, since the solution is working on one server, but not on another one, unless I go to the Alert Management of the User und save the setup of the alert without making any changes on it! After that it works, which is really confusing to me.

I don't know what I did wrong, but can it be the fact, that I was using newAlert.AlertType = SPAlertType.Discussion; instead of newAlert.AlertType = SPAlertType.List; which you used in your article?

How can I look into the DB to check my values against your DB values? I think this could help me a lot in solving my problem.

Hope you can help me on that problem with your experience you made while writing this great article.

Downey said...

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

themahg said...

Hi Jerôme,
If I want to create an alert whan a SocialComment is posted on publishing site. How can I handler this ?