Simple SharePoint Timer Job

December 11, 2008

Tools

Visual Studio 2005
Timer Job Template
The Timer Job Template may also be available online here.

Timer Job Setup

Drop the Timer Job Template (customsharepointjob.zip) into the C:\Documents and Settings\<userlogin>\My Documents\Visual Studio 2005\Templates\ProjectTemplates directory.  You do not need to unzip the file as VS will read the template as is as long as it’s in that folder.

Create Timer Job Project

1. Visual Studio 2005/File/New/Project/Visual C#/Custom SharePoint Job (under My Templates)
2. Give it a name
3. If you get Security Warning, select Load project normally/OK

Code Timer Job

1. Open the .cs file containing the Execute function (not the Installer .cs file)
2. Type code in the Execute function.

Sample Code For Testing:

// get a reference to the current site collection’s content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
// get a reference to the “Tasks” list in the RootWeb of the first site collection in the content database
SPList taskList = contentDb.Sites[0].RootWeb.Lists[”Tasks”];

// create a new task, set the Title to the current day/time, and update the item
SPListItem newTask = taskList.Items.Add();
newTask[”Title”] = DateTime.Now.ToString();
newTask.Update();
Sample Code For Testing (using SPSite and System.IO namespace):
try
{
SPSite mySite = new SPSite(”http://mossdev”);
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists[”Tasks”];
int intCounter = 0;
foreach (SPListItem myItem in myList.Items)
intCounter++;
SPListItem newTask = myList.Items.Add();
newTask[”Title”] = “Counter of Items at ” +
DateTime.Now.ToString();
newTask.Update();

}
catch (Exception ex)
{
TextWriter myWriter = new StreamWriter(@”c:\CountDocsJobError.txt”);
myWriter.WriteLine(ex.ToString());
myWriter.Close();
}

3.  Open the Installer .cs file.
4.  In the FeatureActivated method, change the schedule.Interval property.  The default is “schedule.Interval = 5″ which fires the timer job every 5 minutes.

You may need to restart the Windows SharePoint Services Timer service by opening the Services console either by going into Windows Admin tools or by typing Services.msc at the Run command.  It also might not hurt to do an iisreset.

Build and Deploy Timer Job

1. Build solution.
2. Run the setup.bat file in the project folder on your local drive (C:\Documents and Settings\<userlogin>\My Documents\Visual Studio 2005\Projects\<timerjobprojectname>\setup.bat ).  The setup.bat will place the files in the proper places.

To check if the project is properly deployed and running: SharePoint 3.0 Central Administration\Operations\Timer Job Definitions to view the solutions and SharePoint 3.0 Central Administration\Operations\Timer Job Status to see if it has succeeded or failed

Timer Job Update and Redeploy

1. Rebuild solution.
2. Run the setup.bat file in the project folder on your local drive (C:\Documents and Settings\<userlogin>\My Documents\Visual Studio 2005\Projects\<timerjobprojectname>\setup.bat ). It will realize that the solution has already been deployed and will automatically retract it.
3. Run setup.bat again to redeploy.

You may need to restart the Windows SharePoint Services Timer service by opening the Services console either by going into Windows Admin tools or by typing Services.msc at the Run command.  It also might not hurt to do an iisreset.

Note: Since timer jobs deal with only fire at set intervals, it is often helpful to test code using other types of projects first.