Click here to Skip to main content
15,896,111 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 594.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.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Mullivan.SharePoint.WebParts
{
    public class MostViewedEditor : EditorPart
    {
        private CheckBox _cboxIsUserQuery = null;
        private DropDownList _ddlSpan = null;
        private DropDownList _ddlReturnCount = null;
        private DropDownList _ddlScope = null;
        private TextBox _tboxExtensions = null;

        public MostViewedEditor(string webPartId)
        {
            this.ID = webPartId + "_LibNavLinksEditor";
        }

        protected override void OnInit(EventArgs e)
        {
            _cboxIsUserQuery = new CheckBox();
            _ddlSpan = new DropDownList();
            _ddlReturnCount = new DropDownList();
            _ddlScope = new DropDownList();
            _tboxExtensions = new TextBox();

            _cboxIsUserQuery.ID = "cboxUserQuery";
            _ddlReturnCount.ID = "ddlReturnCount";
            _ddlSpan.ID = "ddlSpan";
            _ddlScope.ID = "ddlScope";
            _tboxExtensions.ID = "tboxExtensions";

            _ddlReturnCount.Items.Add("5");
            _ddlReturnCount.Items.Add("10");
            _ddlReturnCount.Items.Add("15");
            _ddlReturnCount.Items.Add("20");
            _ddlReturnCount.SelectedValue = "10";

            _ddlSpan.Items.Add("1");
            _ddlSpan.Items.Add("7");
            _ddlSpan.Items.Add("14");
            _ddlSpan.Items.Add("30");
            _ddlSpan.SelectedValue = "7";

            ListItem liTopLevelSite = new ListItem("Top Level Site", "TopLevelSite");
            ListItem liCurrentSite = new ListItem("This Site", "CurrentSite");
            _ddlScope.Items.Add(liTopLevelSite);
            _ddlScope.Items.Add(liCurrentSite);

            _ddlScope.SelectedValue = "CurrentSite";

            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {

            #region "HTML"
            Literal litBeginHtml = new Literal();
            litBeginHtml.Text = @"
<br />
<div>
<table cellspacing=""0"" cellpadding=""0"" border=""0"" style=""width:0px; width:100%;border-collapse:collapse;"">
	<tr>
		<td>
            <div class=""UserSectionTitle"">
                <a id=""{0}_MVQueryCategory_IMAGEANCHOR"" TabIndex=""0"" onkeydown='MSOMenu_KeyboardClick(this, 13, 32);' 
                style='cursor:hand' 
                onclick=""javascript:MSOTlPn_ToggleDisplay('{0}_MVQueryCategory', '{0}_MVQueryCategory_IMAGE', '{0}_MVQueryCategory_ANCHOR', 'Expand category: Query', 'Collapse category: Query','{0}_MVQueryCategory_IMAGEANCHOR');"" 
                title=""Collapse category: Query"" >
                    &nbsp;<img id=""{0}_MVQueryCategory_IMAGE"" alt=""Collapse category: Query"" border=""0"" src=""/_layouts/images/TPMin2.gif"" />&nbsp;
                </a>
                <a TabIndex=""-1"" 
                    onkeydown='MSOMenu_KeyboardClick(this, 13, 32);' 
                    id=""{0}_MVQueryCategory_ANCHOR"" 
                    style='cursor:hand' 
                    onclick=""javascript:MSOTlPn_ToggleDisplay('{0}_MVQueryCategory', '{0}_MVQueryCategory_IMAGE', '{0}_MVQueryCategory_ANCHOR', 'Expand category: Query', 'Collapse category: Query','{0}_MVQueryCategory_IMAGEANCHOR');"" 
                    title=""Collapse category: Query"" >&nbsp;Query
                </a>
            </div>
        </td>
    </tr>
</table>
<div id=""{0}_MVQueryCategory"">
    <table cellspacing=""0"" border=""0"" style=""border-width:0px;width:100%;border-collapse:collapse;"">";
            Literal litEndHtml = new Literal();
            litEndHtml.Text = @"
    </table>
</div>";
            #endregion "HTML"

            this.Controls.Add(litBeginHtml);

            AppendEditControl("User Only", "If set to true, the query will only display the most viewed items by the current user.", _cboxIsUserQuery, this.Controls);
            AppendEditControl("Scope", "", _ddlScope, this.Controls);
            AppendEditControl("Return Count", "The amount of items returned.", _ddlReturnCount, this.Controls);
            AppendEditControl("Span (Days)", "The amount of days back the query should go to determine the hit count for each item.", _ddlSpan, this.Controls);
            AppendEditControl("Extensions", "A collection of document extensions that determine what types of content should be returned. (Seperate with a comma for multiples).", _tboxExtensions, this.Controls);

            this.Controls.Add(litEndHtml);

            base.CreateChildControls();
        }

        private void AppendEditControl(string displayName, string description, WebControl control, ControlCollection controlCollection)
        {
            Literal litBeginHtml = new Literal();
            Literal litEndHtml = new Literal();

            controlCollection.Add(litBeginHtml);
            controlCollection.Add(control);
            controlCollection.Add(litEndHtml);


            litBeginHtml.Text = string.Format(@"
		<tr>
			<td>
			    <div class=""UserSectionHead"">
			        <LABEL FOR=""{0}"" 
			               TITLE=""{2}"">{1}
			        </LABEL>
			    </div>
			    <div class=""UserSectionBody"">
			        <div class=""UserControlGroup"">
			            <nobr>", control.ClientID, displayName, description);
            litEndHtml.Text = @"
                        </nobr>
			        </div>
			    </div>
			    <div style='width:100%' class='UserDottedLine'>
			    </div>
		    </td>
	    </tr>";

            control.CssClass = "UserInput";
            control.Style[HtmlTextWriterStyle.Width] = "176px";
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            MostViewedWebPart mvwp = this.WebPartToEdit as MostViewedWebPart;
            if (mvwp == null)
                return false;

            mvwp.Scope = (MostViewedWebPart.MostViewedScope)Enum.Parse(typeof(MostViewedWebPart.MostViewedScope), _ddlScope.SelectedValue);
            mvwp.ReturnCount = int.Parse(_ddlReturnCount.SelectedValue);
            mvwp.SpanDays = int.Parse(_ddlSpan.SelectedValue);
            mvwp.IsUserQuery = _cboxIsUserQuery.Checked;
            if (_tboxExtensions.Text.Trim() != string.Empty)
            {
                List<string> exts = new List<string>(_tboxExtensions.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                for (int i = 0; i < exts.Count; i++)
                {
                    exts[i] = exts[i].Trim();
                    if (string.IsNullOrEmpty(exts[i]))
                    {
                        exts.RemoveAt(i);
                        i--;
                    }
                }
                if (exts.Count > 0)
                    mvwp.Extensions = exts.ToArray();
                else
                    mvwp.Extensions = null;
            }
            else
                mvwp.Extensions = null;

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();

            MostViewedWebPart mvwp = this.WebPartToEdit as MostViewedWebPart;
            if (mvwp == null)
                return;
            _tboxExtensions.Text = "";
            _ddlScope.SelectedValue = mvwp.Scope.ToString();
            _ddlReturnCount.SelectedValue = mvwp.ReturnCount.ToString();
            _ddlSpan.SelectedValue = mvwp.SpanDays.ToString();
            _cboxIsUserQuery.Checked = mvwp.IsUserQuery;
            if (mvwp.Extensions != null)
            {
                for (int i = 0; i < mvwp.Extensions.Length; i++)
                {
                    if (i != 0)
                        _tboxExtensions.Text += ", ";
                    _tboxExtensions.Text += mvwp.Extensions[i];
                }
            }
        }
    }
}

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