Monday, September 22, 2008

Executing any Timer job using code

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using Microsoft.SharePoint.Administration;

public ExecuteSharePointJob(string sJobName)
{
SPSite site = new SPSite("http://localhost");
//foreach (SPService srv in SPFarm.Local.Services)
//{
//foreach (SPJobDefinition job in srv.JobDefinitions)
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
string jobTitle = job.Title;

if (jobTitle == JobName)
{
Trace.WriteLine("***************** Start of execution for job");
job.Execute(new Guid("PassTheContentDBGUID"));
Trace.WriteLine("***************** End of execution for job");
}
}
//}
}

0 comments: