Thursday, September 10, 2009

How to Create Job Sceduler in Sharepoint?

How to Create Job Sceduler in Sharepoint?
Create one custom StsadmCommand using ISPStsadmCommand class. And implement Run method, this method will create/Schedule the job. This STSADM calls once while installing product. This will create jobscheduler.(Here scheduler calls every 50 min)

Example:

//This custom STSADM call once while installing product
public class CustomSTSADM : ISPStsadmCommand
{
public int Run(string command, StringDictionary keyValues, out string output)
{
CustomJob.CreateMyJob(URL);
}
}
// SPJobDefinition class
public class CustomJob_Class : SPJobDefinition
{
//Need to override
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void Execute(Guid ContentDataBaseID)
{
//TODO: Write what should be done by this job every defined time.
}


//Create JobSchedule for 50 min
Public CustomJob(string Url)
{
SPSite mySite = new SPSite(Url);
SPWebApplication application = mySite.WebApplication;
SPJobDefinitionCollection jobsColl =
application.JobDefinitions;

CustomJob_Class customJob = new CustomJob_Class("CustomJob ",
application);

customJob.Title = " Custom Job ";

SPMinuteSchedule mySchedule = new SPMinuteSchedule();
mySchedule.BeginSecond = 0;
mySchedule.EndSecond = 59;
mySchedule.Interval = 50;
customJob.Schedule = mySchedule;

jobsColl.Add(customJob);
application.Update();
}
}

No comments: