Click here to Skip to main content
15,886,689 members
Articles / Web Development / ASP.NET

Send scheduled Reminder/Alerts by email in SharePoint

Rate me:
Please Sign up or sign in to vote.
4.87/5 (14 votes)
24 Mar 2009CPOL12 min read 590.5K   2.1K   67  
Learn how to create a SharePoint Job that queries lists and sends results via email.
using System;
using System.Collections.Generic;
using System.Text;
using Mullivan.Serialization;
using Mullivan.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using System.Security;

namespace Mullivan.SharePoint.Reminders
{
    public class RmdConfiguration : IXmlSerializable
    {
        public RmdConfiguration()
        {
            this.Id = Guid.NewGuid();
            this.LastRun = DateTime.MinValue;
            this.Type = RmdType.Item;
            this.Recurrence = new RmdHourlyRecurrence();
            this.ViewFields = string.Empty;
        }


        public Guid Id
        {
            get;
            set;
        }

        /// <summary>
        /// The name used for the unique identification of the configuration.
        /// </summary>
        public string DisplayName
        {
            get;
            set;
        }

        /// <summary>
        /// Represents if the Reminder should send an email per Item or send as
        /// a bulk email with all the items.
        /// </summary>
        public RmdType Type
        {
            get;
            set;
        }

        /// <summary>
        /// The fields to return from query
        /// </summary>
        public string ViewFields
        {
            get;
            set;
        }

        /// <summary>
        /// Lets the processor know if it should send emails when there are no results
        /// </summary>
        public bool SendNoResults
        {
            get;
            set;
        }

        /// <summary>
        /// Array of email addresses to email to.
        /// </summary>
        public string[] MailTo
        {
            get;
            set;
        }

        /// <summary>
        /// The subject of the email.
        /// </summary>
        public string Subject
        {
            get;
            set;
        }

        /// <summary>
        /// The object that represents how often the engine needs to process this reminder.
        /// </summary>
        public RmdRecurrence Recurrence
        {
            get;
            set;
        }

        /// <summary>
        /// Caml query used to filter items from the list.
        /// </summary>
        public string Query
        {
            get;
            set;
        }

        /// <summary>
        /// The XSL that is used to transform the items returned in the query to an HTML body
        /// for the email.
        /// </summary>
        public string XslTransform
        {
            get;
            set;
        }

        /// <summary>
        /// Used to mark the last time the reminder was run.
        /// </summary>
        public DateTime LastRun
        {
            get;
            set;
        }

        #region IXmlSerializable Members

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();
            if (wasEmpty)
                return;

            reader.MoveToContent();

            reader.ReadStartElement("Id");
            this.Id = GenericSerializer.Deserialize<Guid>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("DisplayName");
            this.DisplayName = GenericSerializer.Deserialize<string>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("Type");
            this.Type = GenericSerializer.Deserialize<RmdType>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("MailTo");
            this.MailTo = GenericSerializer.Deserialize<string[]>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("Subject");
            this.Subject = GenericSerializer.Deserialize<string>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("Recurrence");
            this.Recurrence = GenericSerializer.Deserialize<RmdRecurrence>(reader
                , RmdRecurrence.GetRecurrenceTypes().ToArray());
            reader.ReadEndElement();
            reader.ReadStartElement("Query");
            this.Query = GenericSerializer.Deserialize<string>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("XslTransform");
            this.XslTransform = GenericSerializer.Deserialize<string>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("LastRun");
            this.LastRun = GenericSerializer.Deserialize<DateTime>(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("ViewFields");
            this.ViewFields = GenericSerializer.Deserialize<string>(reader);
            reader.ReadEndElement();


            //Read Closing Element
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("Id");
            GenericSerializer.Serialize<Guid>(this.Id, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("DisplayName");
            GenericSerializer.Serialize<string>(this.DisplayName, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("Type");
            GenericSerializer.Serialize<RmdType>(this.Type, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("MailTo");
            GenericSerializer.Serialize<string[]>(this.MailTo, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("Subject");
            GenericSerializer.Serialize<string>(this.Subject, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("Recurrence");
            GenericSerializer.Serialize<RmdRecurrence>(this.Recurrence, writer
                , RmdRecurrence.GetRecurrenceTypes().ToArray());
            writer.WriteEndElement();
            writer.WriteStartElement("Query");
            GenericSerializer.Serialize<string>(this.Query, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("XslTransform");
            GenericSerializer.Serialize<string>(this.XslTransform, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("LastRun");
            GenericSerializer.Serialize<DateTime>(this.LastRun, writer);
            writer.WriteEndElement();
            writer.WriteStartElement("ViewFields");
            GenericSerializer.Serialize<string>(this.ViewFields, writer);
            writer.WriteEndElement();
        }

        #endregion

        /// <summary>
        /// Deserializes the string and returns the associated object.
        /// </summary>
        /// <param name="strConfiguration"></param>
        /// <returns></returns>
        public static List<RmdConfiguration> ReadConfigurations(string strConfiguration)
        {
            return GenericSerializer.Deserialize<List<RmdConfiguration>>(strConfiguration);
        }

        /// <summary>
        /// Serializes the object into a string so it can be used for storage.
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static string WriteConfigurations(List<RmdConfiguration> config)
        {
            return GenericSerializer.Serialize<List<RmdConfiguration>>(config);
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions