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);
                }
            }

        }
}
You will need to import “System.Net.Mail” and get the mail server/port info, but it gives you the basis of accomplishing a very flexible solution for sending emails from an updated list. A possible use is to send notifications from Project Server 2007 when Issues/Risks are updated. The Event Receiver above will send emails to the users that are defined in the list item itself without having to create another list for lookup or managing a sharepoint group which usually required elevated privileges.
Enjoy!