Implementing Alerts in Sharepoint using SPAlert


We had a requirement in one of our projects. When users reply a discussion or post a comment, we wanted to send a notification to the user who has originally posted the discussion.


This can be implemented using SPAlert.


Here are the required steps.


1. Create an event handler that recieves the addition and updation actions done to any item added or edited in a discussion forum by the user.


2. Add to the alerts of the user , an alert that defines the item he want to be alerted when changed ... that means , if the user added a new discussion he will be alerted when any change is done to this item , or if the user commented or replied to any other discussion he will be alerted when any change is done to the item he replied to...


the code is very easy as we will see...


To see how to make event handlers please see this post... http://sptechytalks.blogspot.com/2009/05/spitemeventreciever-how-to-implement.html


then now we will see the code that makes the previous requirements we have listed...


the feature.xml :

the Element.xml:

the Asp.net code that makes the logic we want:


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;

}

}

}


If there is anything you do not understand, please drop me a comment here.



Ebook Download
View all
Learn
View all