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!
I am trying to run the executable on my sharepoint web front end and get a message :
——————————————————————
An error has occurred:
System.NullReferenceException: Object reference not set to an instance of an obj
ect.
at ImmediateAlerts.Program.Main(String[] args) in C:TFSImmediateAlertsImme
diateAlertsProgram.cs:line 21
Does the executable require an argument, such as the website name or database?
In your code, what is line 21?
John i’m getting the same error as Andy. I looked over the code and it doesn’t look like it should require a website or sitecollectionurl but my line 21 looks identical to yours since I am just trying to run the code I downloaded from here and compiled. I’m running it from one of my front-end servers and compiled it on our dev environment so it could use all the Sharepoint references etc.
Any suggestions?
Also, will this cause task ownership e-mails to go out immediately? I have been trying to get those to email faster all day, I even changed all the workflow and job-immediate-alerts timers on our farm to no avail.
I’m not sure why SPWebService service = farm.Services.GetValue(“”); would come back null.
This code kicks off the immediate alerts job. So, yes it make it so you can speed up the sending of these emails throughout the day.