Click here to Skip to main content
15,886,258 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 System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.SharePoint;
using Mullivan.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Xml;
using System.Web;
using Microsoft.SharePoint.WebPartPages.Communication;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;
using Microsoft.SharePoint.Administration;

namespace Mullivan.SharePoint.WebParts
{
    [GuidAttribute("90f70852-4944-4979-8bad-35f177b7bbbb")]
    [XmlRoot(Namespace = "ListQueryResultsWebPart")]
    public class ListQueryResultsWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private const string DEFAULT_XSLT = "";
        private ListViewWebPart _listView = null;

        public ListQueryResultsWebPart() 
            : base()
        {

        }

        public ListQueryData CurrentQuery
        {
            get
            {
                return (ListQueryData)this.ViewState["CurrentQuery"];
            }
            set
            {
                this.ViewState["CurrentQuery"] = value;
            }
        }

        [ConnectionConsumer("List Query Consumer", "listQueryProvider")]
        public void SetQueryData(ListQueryData data)
        {
            this.CurrentQuery = data;
            ExecuteQuery();
        }

        private void ExecuteQuery()
        {
            EnsureChildControls();
            ListQueryData data = this.CurrentQuery;
            if (data != null)
            {
                HttpRequest request = this.Page.Request;
                _listView = new ListViewWebPart();
                _listView.ID = "listView";
                _listView.ChromeType = PartChromeType.Default;
                _listView.ChromeState = PartChromeState.Normal;
                _listView.ViewType = ViewType.Html;
                _listView.WebId = data.WebId;
                _listView.ListName = data.ListId.ToString("B").ToUpper(); ;
                _listView.ListViewXml = GetListViewXml(data, data.Query, data.ViewFields);
                _listView.GetDesignTimeHeader();
                _listView.GetDesignTimeHtml();
                _listView.GetDesignTimeFooter();

                this.Controls.Add(_listView);

            }
        }

        private string GetListViewXml(ListQueryData data, string Query, string View)
        {
            SPSite site = SPControl.GetContextSite(this.Context);
            using (SPWeb web = site.OpenWeb(data.WebId))
            {
              
                SPList list = web.Lists[data.ListId];
                string listViewXml = Properties.Resources.ListView;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(listViewXml);

                //Have to set the default view name otherwise paging doens't work
                xDoc.DocumentElement.SetAttribute("Name", list.DefaultView.ID.ToString());
                xDoc.DocumentElement.SetAttribute("DisplayName", "Search View");
                xDoc.DocumentElement.SetAttribute("Url", list.DefaultView.Url);
                xDoc.DocumentElement.SetAttribute("ImageUrl", list.ImageUrl);

                XmlElement xeView = (XmlElement)xDoc.DocumentElement.SelectSingleNode("ViewFields");
                xeView.InnerXml = View;

                XmlElement xeQuery = (XmlElement)xDoc.DocumentElement.SelectSingleNode("Query");
                xeQuery.InnerXml = Query;

                XmlElement xeRowLimit = (XmlElement)xDoc.DocumentElement.SelectSingleNode("RowLimit");
                xeRowLimit.InnerText = data.PageSize.ToString();

                return xDoc.InnerXml;
            }
        }


    }
}

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