Export (0) Print
Expand All

SPRunningJob class

A running job object is created for every instance of the job that is executing, at the rate of one per server.

System.Object
  Microsoft.SharePoint.Administration.SPRunningJob

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public sealed class SPRunningJob

If the running job is active on at least one server within the farm, it is marked as Active.

A one-time job that completes successfully is deleted from the table of running jobs. Its job definition is also deleted.

The following code example iterates through the services on a SharePoint Foundation farm to display information about each job that is currently running for each service. For SPRunningJob objects, all programmatic interaction is with properties; there are methods available in the SPRunningJobCollection object that allow the implementer to interact with a specific instance of a SPRunningJob object.

[C#]

SPFarm farm = SPFarm.Local.Farm;
SPServiceCollection services = SPFarm.Local.Services;
int rand_job;
Guid jobdefid;
SPRunningJob rj;
string servername;
string svc_name = string.Empty;
 foreach (SPService service in services) {
  SPRunningJobCollection runningJobs = service.RunningJobs;
  if (runningJobs.Count > 0) {
    if (svc_name == string.Empty) {
      svc_name = service.Name;
    }
    Console.WriteLine("****Job Collection Count is     " + runningJobs.Count);
    Console.WriteLine("****Job Collection Parent is    " + runningJobs.Parent);
    Console.WriteLine("****Job Collection Service is   " + runningJobs.Service);
    Console.WriteLine("****Job Collection Web App is   " + runningJobs.WebApplication);
    // For the GetInstance method, save off job information at random
    rand_job = runningJobs.Count / 2;
    Console.WriteLine("****Collection member " + rand_job + " is " + runningJobs[rand_job].JobDefinition);
    jobdefid = runningJobs[rand_job].JobDefinitionId;
    servername = runningJobs[rand_job].ServerName;
  }
  else {
    jobdefid = Guid.Empty;
    servername = null;
  }
  foreach (SPRunningJob runningJob in runningJobs) {
    Console.WriteLine("****Job Definition is   " + runningJob.JobDefinition);
    Console.WriteLine("****Job Id is           " + runningJob.JobDefinitionId);
    Console.WriteLine("****Job Title is        " + runningJob.JobDefinitionTitle);
    Console.WriteLine("****Parent is           " + runningJob.Parent);
    Console.WriteLine("****Percentage Done is  " + runningJob.PercentageDone);
    Console.WriteLine("****Server Name is      " + runningJob.ServerName);
    Console.WriteLine("****Service Id is       " + runningJob.ServiceId);
    Console.WriteLine("****Start Time is       " + runningJob.StartTime);
    Console.WriteLine("****Status is           " + runningJob.Status);
    Console.WriteLine("****Web App Id is       " + runningJob.WebApplicationId);
    Console.WriteLine("****");
  }
  if (jobdefid != Guid.Empty) {
    // random jobdef from collection 
    // getinstance method
    rj = runningJobs.GetInstance(jobdefid, servername);
    Console.WriteLine("*@@* Job Definition is   " + rj.JobDefinition);
    Console.WriteLine("*@@* Job Id is           " + rj.JobDefinitionId);
    Console.WriteLine("*@@* Job Title is        " + rj.JobDefinitionTitle);
  }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Reference

Other resources

Creating Custom Timer Jobs in Windows SharePoint Services 3.0

Community Additions

ADD
Show:
© 2014 Microsoft