Wednesday, October 1, 2008

Sending email to user few days before document expires

Scenario:
Out-of-Box Expiration policy simply delete the document without sending any confirmation few days before Expiration Date is reached

Solution:
You can write a console application and schedule it to Run every night. This application will basically check if any document is expiring in next 30 days and will send an email to the Last Modified User

Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ExpiringDocumentsEmail
{
class Program
{
static void Main(string[] args)
{
try
{
string sSiteUrl = ConfigurationManager.AppSettings["Site"];
string[] sLists = COnfigurationManager.AppSettings["Lists"]).Split(',');
string sDateColumnName = ConfigurationManager.AppSettings["DateColumnName"];
string sEmailToColumnName = ConfigurationManager.AppSettings["EmailToColumnName"];
string sMailSubject = ConfigurationManager.AppSettings["Subject"];
string sGapInDays = ConfigurationManager.AppSettings["GapInDays"];
string sViewName = ConfigurationManager.AppSettings["ViewName"];

using (SPSite oSPSite = new SPSite(sSiteUrl))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{
foreach (string listName in sLists)
{
SPList oSPList = oSPWeb.Lists[listName];

foreach (SPListItem oSPListItem in oSPList.Items)
{
System.TimeSpan dateDiff = new System.TimeSpan(((DateTime)oSPListItem[sDateColumnName]).Ticks - DateTime.Now.Ticks);

// Console.WriteLine(((long)dateDiff.Days).ToString());

if ((long)dateDiff.Days == int.Parse(sGapInDays))
{
SPUser user = oSPWeb.SiteUsers.GetByID(Convert.ToInt32(oSPListItem[sEmailToColumnName].ToString().Split(';')[0]));
if (user.Email.ToString().Length > 0)
{
SendEmail(user.Email.ToString(), sMailSubject, oSPSite.Url + oSPList.Views[sViewName].ServerRelativeUrl);
}
}
}
}
}
}

// Console.Read();
}
catch ( Exception ex )
{
// Console.WriteLine(ex.ToString());
// Console.Read();
}
}

public static void SendEmail(string toAddress, string subject, string ListUrl)
{

string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
string fromUser = System.Configuration.ConfigurationManager.AppSettings["EmailFrom"];

MailMessage message = new MailMessage(fromUser, toAddress);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = "You can navigate to the expiring document by click " + "<a href=\"" + ListUrl + "\">here</a>. This is an auto generated email. Please do not reply.";

try
{
SmtpClient client = new SmtpClient(smtpServer);
//client.Send(message);
client.SendAsync(message,"mail sent.");
}
catch { }
}
}
}
Application Configuration (App.Config):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Site" value="http://localhost/"/>
<add key="Lists" value="Shared Documents,Client Documents"/>
<add key="ViewName" value="Expiring"/>
<add key="GapInDays" value="30"/>
<add key="DateColumnName" value="Expiring Date"/>
<add key="EmailToColumnName" value="Modified By"/>
<add key="Subject" value="Document expires in 30 days"/>
<add key="SmtpServer" value="mailhost.training.com"/>
<add key="EmailFrom" value="admin@training.com"/>
</appSettings>
</configuration>

0 comments: