using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
namespace SPAlertsCollection
{
public class SPAlertOfDiscussionForum : SPItemEventReceiver
{
// code done when the discussion is added
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
AlertMeOnPosting(properties);
}
// code done when a reply is done to the discussion
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
AlertMeOnPosting(properties);
}
// code will be done to alert the user when posting is done
public void AlertMeOnPosting(SPItemEventProperties properties)
{
bool userAlerted = false ;
// here we connect again to the moss site because inside the event handler we cant see the SPContext so we must connect again to the moss site
SPSite mossSite = new SPSite(properties.SiteId);
SPWeb mossWeb = mossSite.RootWeb;
// we get the current user working on the site.
SPUser currentUser = mossWeb.CurrentUser;
// here we need to check if the user is already assigned to this kind of alerts , what means he will be alerted from this list or notuserAlerted=CheckIfUserAssignedToAlert(currentUser,properties);
// if not assigned to this alert , he will be assigned
if (!userAlerted)
{
AssignAlertToUser(properties,currentUser.Alerts);
}
}
//this function checks whether the user is assigned to this kind of alerts or not by looping on all his assigned alerts and comparing the title of the current alert with the titles of his already assigned alerts
private bool CheckIfUserAssignedToAlert(SPUser currentUser, SPItemEventProperties properties)
{
// here we call the function that returns the title formatted into the way the alerts title are formatted in
string newAlertTitle = FormatTitleOfAlert(properties);
// loop into the alerts assigned to the user and sees if he is assigned to this alert from this list and from this item specifically or not
for (int i = 0; i <>
{
if (currentUser.Alerts[i].Title == newAlertTitle)
{
return true;
}
}
return false;
}
// if user not assigned to alerts he will be assigned.
private void AssignAlertToUser(SPItemEventProperties properties, SPAlertCollection sPAlertCollection)
{
// here we will add the alert to the user defining the item will be alerted on any change done to it and defining the event type and alert type . after doing that successfully a mail is sent to the user notifying him that he is assigned to receive alerts from this list and this item specifically.
SPAlert alert = sPAlertCollection.Add();
alert.AlertType = SPAlertType.Item;
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.Item = properties.ListItem;
alert.EventType = SPEventType.Discussion;
alert.Update();
}
// this is the function that formats the alert title. To be in the following format ListName: Item ID’Id’ say that DocLib: Item ID7
Or DocLib if the item is newely created by the User and there is no replies yet
private string FormatTitleOfAlert(SPItemEventProperties properties)
{
string newTitle=string.Empty;
if(properties.ListItem==null)
{
newTitle=properties.ListTitle;
}
else
{
newTitle = properties.ListTitle + ": Item ID" + properties.ListItemId;
}
return newTitle;
}
}
}