For SharePoint 2007 I had a need to write a console application that fires the Immediate Alerts timer job manually. The Immediate Alerts timer job executes every 5 minutes by default to send out the email alerts setup by users in SharePoint. For most cases, simply changing the interval of the timer service with stsadm would suffice. TechNet has instructions for seting the interval per site: http://technet.microsoft.com/en-us/library/cc262432(office.12).aspx. In my case I needed to do some debugging and used a custom console application instead and am posting it here to share. It can be repurposed to any of the SharePoint timer jobs, but used the “job-immediate-alerts” job as an example.

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ImmediateAlerts
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“——————————————————————“);
Console.WriteLine(“Start Immediate Alerts Timer Job – ” + DateTime.Now.ToString());
Console.WriteLine(“——————————————————————“);

try
{
SPFarm farm = SPFarm.Local;
SPWebService service = farm.Services.GetValue(“”);

foreach (SPWebApplication webApp in service.WebApplications)
{
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == “job-immediate-alerts”)
{
foreach (SPContentDatabase db in webApp.ContentDatabases)
{
Console.WriteLine(“Starting ‘job-immediate-alerts’ for Database: ‘{0}'”, db.Name);
try
{
job.Execute(db.Id);
}
catch (Exception ex)
{
Console.WriteLine(“An error has occurred:”);
Console.WriteLine(ex.ToString());
}
Console.WriteLine(“Done”);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(“An error has occurred:”);
Console.WriteLine(ex.ToString());
}

Console.WriteLine(“——————————————————————“);
Console.WriteLine(“End Immediate Alerts Timer Job”);
Console.WriteLine(“——————————————————————“);
Console.WriteLine(“”);
}
}
}
[/csharp]

The application can be run simply by running the compiled executable. If you desire to capture the output, you can simply do so by specifying the output be directed to a file rather than the screen:

[shell]ImmediateAlerts.exe >> output.log[/shell]

You can download my source code here: ImmediateAlerts.zip

Happy Coding!


Pin It on Pinterest

Share This
%d bloggers like this: