Sending Multiple Notifications in SharePoint
In Sharepoint there are many ways of notifying a user when an item gets updated on a list.
1. Setting up an alert.
2. Subscribing to an RSS Feed.
3. Using workflow via the List Settings, Sharepoint Designer or Visual Studio.
All these mechanisms have their pros and cons, but there is one thing that is not supported by any of them: Sending a notification to users when they are set up in a multi-choice person list within the SAME list. Additionally, 1 and 2 above require the user to set this up manually on their own, while 3 is much more complicated and has limitations. I chose to write my own Event Receiver in order to accomplish this task.
I won’t go into the details of creating an Event Receiver in Sharepoint and publishing it, since there is already a lot of documentation on this on the internet. In my example, the List Name is called “MyDocs” and the field that contains the users is called “Notifiers” (note: this field can contains a multiple users).
public class MyDocsItemEventReceiver : SPItemEventReceiver
{
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
DisableEventFiring();
try
{
SPListItem myDocsItem = properties.ListItem;
var notifiers = myDocsItem["Notifiers"] as SPFieldUserValueCollection;
SendEmailToNotifiers(notifiers);
}
finally
{
EnableEventFiring();
}
}
private void SendEmailToNotifiers(SPFieldUserValueCollection _notifiers)
{
if (_notifiers != null)
{
List<string> _notifierList = new List<string>();
foreach (SPFieldUserValue _notifier in _notifiers)
{
if (!string.IsNullOrEmpty(_notifier.User.Email))
_notifierList.Add(_notifier.User.Email.Trim());
}
if (_notifierList.Count > 0)
{
SmtpClient _smtpClient = new SmtpClient();
_smtpClient.Host = "email_server_name";
_smtpClient.Port = 25;
_smtpClient.EnableSsl = false;
MailMessage _mailMessage = new MailMessage();
foreach (string _reviewer in _notifierList)
{
_mailMessage.To.Add(new MailAddress(_reviewer));
}
_mailMessage.From = new MailAddress("fromaddress@company.com");
_mailMessage.Subject = "the subject line";
_mailMessage.Body = "the body of the email";
_smtpClient.Send(_mailMessage);
}
}
}
}

Leave a Reply