Click here to Skip to main content
15,885,537 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.3K   2.1K   67  
Learn how to create a SharePoint Job that queries lists and sends results via email.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Mullivan.Collections.Generic;
using Mullivan.SharePoint.Reminders;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;

namespace Mullivan.SharePoint.Pages
{
    public partial class _ReminderSettings : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        private List<RmdConfiguration> _rmdConfigs;
        private SPWeb _spWeb = null;
        private SPList _spList = null;
        private SPBaseType _baseType;
        private SPListTemplateType _templateType;
        private string _strDefaultViewUrl;

        // Properties
        protected override SPBasePermissions RightsRequired
        {
            get
            {
                return (SPBasePermissions.EmptyMask | SPBasePermissions.ManageLists);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ClientScriptManager csm = this.Page.ClientScript;
            Type lnType = typeof(_ReminderSettings);

            ibtnEdit.ImageUrl = "~" + ResolveUrl(csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.Pages.Images.Edit.png"));
            ibtnAdd.ImageUrl = "~" + ResolveUrl(csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.Pages.Images.Add.png"));
            ibtnDelete.ImageUrl = "~" + ResolveUrl(csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.Pages.Images.Remove.png"));
            ibtnMoveUp.ImageUrl = "~" + ResolveUrl(csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.Pages.Images.ArrowUp.png"));
            ibtnMoveDown.ImageUrl = "~" + ResolveUrl(csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.Pages.Images.ArrowDown.png"));

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ThrowIfNoListQueryString();

            _spWeb = SPControl.GetContextWeb(HttpContext.Current);
            _spList = _spWeb.Lists.GetList(new Guid(this.Request.QueryString.GetValues("List")[0]), true);
            _rmdConfigs = RmdUtil.GetRmdConfigs(_spList);
            if (_rmdConfigs == null)
                _rmdConfigs = new List<RmdConfiguration>();

            _baseType = this._spList.BaseType;
            _templateType = this._spList.BaseTemplate;
            _strDefaultViewUrl = this._spList.DefaultViewUrl;
            if (_templateType == SPListTemplateType.UserInformation)
            {
                if (!(base.Web.ServerRelativeUrl == "/"))
                    _strDefaultViewUrl = base.Web.ServerRelativeUrl + "/_layouts/people.aspx";
                else
                    _strDefaultViewUrl = base.Web.ServerRelativeUrl + "_layouts/people.aspx";
            }

            if (!Page.IsPostBack)
            {
                LoadRemindersListBox();
            }

            base.PageTarget = this._spList;
            this.onetidListEditTitleLink.NavigateUrl = SPHttpUtility.UrlPathEncode(this._strDefaultViewUrl, true);
            this.onetidListEditTitleLink.Text = SPHttpUtility.HtmlEncode(this._spList.Title);
        }

        private void LoadRemindersListBox()
        {
            lboxReminders.Items.Clear();
            foreach (RmdConfiguration rmdConfig in _rmdConfigs)
            {
                lboxReminders.Items.Add(new ListItem(rmdConfig.DisplayName, rmdConfig.Id.ToString()));
            }
        }

        protected void ibtnEdit_Click(object sender, ImageClickEventArgs e)
        {
            Guid guidSelectedRmd = Guid.Empty;

            if (string.IsNullOrEmpty(lboxReminders.SelectedValue))
                return;
            else
                guidSelectedRmd = new Guid(lboxReminders.SelectedValue);

            string url = string.Format("~{0}/_layouts/EditReminderSettings.aspx?List={1}&Reminder={2}",
               base.Web.ServerRelativeUrl, this._spList.ID, guidSelectedRmd);
            Response.Redirect(url);
        }

        protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            string url = string.Format("~{0}/_layouts/EditReminderSettings.aspx?List={1}&Reminder={2}",
                base.Web.ServerRelativeUrl, this._spList.ID, Guid.Empty);
            Response.Redirect(url);
        }

        protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            Guid guidSelectedRmd = Guid.Empty;

            if (string.IsNullOrEmpty(lboxReminders.SelectedValue))
                return;
            else
                guidSelectedRmd = new Guid(lboxReminders.SelectedValue);

            RmdConfiguration rmdConfig = RmdUtil.FindReminder(guidSelectedRmd, _rmdConfigs);
            if (rmdConfig != null)
            {
                _rmdConfigs.Remove(rmdConfig);
                RmdUtil.SetRmdConfigs(_spList, _rmdConfigs);
                LoadRemindersListBox();
            }
        }

        protected void ibtnMoveUp_Click(object sender, ImageClickEventArgs e)
        {
            Guid guidSelectedRmd = Guid.Empty;

            if (string.IsNullOrEmpty(lboxReminders.SelectedValue))
                return;
            else
                guidSelectedRmd = new Guid(lboxReminders.SelectedValue);

            RmdConfiguration rmdConfig = RmdUtil.FindReminder(guidSelectedRmd, _rmdConfigs);
            int idx = _rmdConfigs.IndexOf(rmdConfig);
            if (rmdConfig != null && idx > 0)
            {
                _rmdConfigs.Remove(rmdConfig);
                _rmdConfigs.Insert(idx - 1, rmdConfig);
                RmdUtil.SetRmdConfigs(_spList, _rmdConfigs);
                LoadRemindersListBox();
                lboxReminders.SelectedValue = guidSelectedRmd.ToString();
            }
        }

        protected void ibtnMoveDown_Click(object sender, ImageClickEventArgs e)
        {
            Guid guidSelectedRmd = Guid.Empty;

            if (string.IsNullOrEmpty(lboxReminders.SelectedValue))
                return;
            else
                guidSelectedRmd = new Guid(lboxReminders.SelectedValue);

            RmdConfiguration rmdConfig = RmdUtil.FindReminder(guidSelectedRmd, _rmdConfigs);
            int idx = _rmdConfigs.IndexOf(rmdConfig);
            if (rmdConfig != null && idx < _rmdConfigs.Count - 1)
            {
                _rmdConfigs.Remove(rmdConfig);
                _rmdConfigs.Insert(idx + 1, rmdConfig);
                RmdUtil.SetRmdConfigs(_spList, _rmdConfigs);
                LoadRemindersListBox();
                lboxReminders.SelectedValue = guidSelectedRmd.ToString();
            }
        }

        public static void ThrowIfNoListQueryString()
        {
            string[] values = HttpContext.Current.Request.QueryString.GetValues("List");
            if ((values == null) || (values.Length == 0))
            {
                throw new SPException(SPResource.GetString("InvalidQueryString", new object[] { "List" }));
            }
        }

    }
}

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