Second Life of a Hungarian SharePoint Geek

March 17, 2010

Enumerating timer jobs for service applications and a new way of forcing the immediate execution of a timer job from code

Filed under: Service applications, SP 2010, Timer jobs — Tags: , , — Peter Holpar @ 00:15

You can enumerate SharePoint timer job definitions using the following method:

  1. private void EnumerateJobs(SPSite site)
  2. {
  3.     foreach (SPJobDefinition jobDefinition in site.WebApplication.JobDefinitions)
  4.     {
  5.         Console.WriteLine("JobDefinition Name ‘{0}’, DisplayName: ‘{1}’, Title: ‘{2}’",
  6.             jobDefinition.Name, jobDefinition.DisplayName, jobDefinition.Title);
  7.     }
  8. }

If the timer job is part of a service application, this method has to be modified a bit:

  1. private void EnumerateServiceJobs(SPSite site)
  2. {
  3.     foreach (SPService service in site.WebApplication.Farm.Services)
  4.     {
  5.         Console.WriteLine("Service Name: ‘{0}’, DisplayName: ‘{1}’, TypeName: ‘{2}’", service.Name, service.DisplayName, service.TypeName);
  6.         foreach (SPJobDefinition jobDefinition in service.JobDefinitions)
  7.         {
  8.             Console.WriteLine("  JobDefinition Name ‘{0}’, DisplayName: ‘{1}’, Title: ‘{2}’",
  9.                 jobDefinition.Name, jobDefinition.DisplayName, jobDefinition.Title);
  10.  
  11.         }
  12.     }
  13. }

As Ayman El-Hattab wrote in his post the timer jobs in SharePoint 2010 can be started immediately from the admin user interface using the Run Now button. Although it can be done in WSS 3.0 from code either using the Execute method or by creating a new schedule on demand, there is a new way to do the same from code in in SharePoint 2010. The SPJobDefinition class has a new method called RunNow. The following code shows how to start a specific job definition immediately by specifying the service type name and the job name:

  1. private void StartServiceJob(string serviceTypeName, string jobName)
  2. {
  3.     foreach (SPService service in _site.WebApplication.Farm.Services)
  4.     {
  5.         if ((serviceTypeName == null) || (serviceTypeName == service.TypeName))
  6.         {
  7.             foreach (SPJobDefinition jobDefinition in service.JobDefinitions)
  8.             {
  9.                 if (jobDefinition.Name == jobName)
  10.                 {
  11.                     jobDefinition.RunNow();
  12.                     break;
  13.                 }
  14.             }
  15.         }
  16.     }
  17. }

Or using just the job name:

  1.         private void StartServiceJob(string jobName)
  2.         {
  3.             StartServiceJob(null, jobName);
  4.         }

The following code line starts the UserProfile – Profile Synchronization Job that is part of the User Profile Service.

StartServiceJob("User Profile Service", "UserProfile_ProfileSynchronizationJob");

Advertisements

3 Comments »

  1. […] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Enumerating timer jobs for service applications and a new way of forcing the immediate execution of … […]

    Pingback by Working with profile subtypes from code « Second Life of a Hungarian SharePoint Geek — March 17, 2010 @ 00:37

  2. […] process that is the UserProfile – Profile Synchronization Job of the User Profile Service. See this post for details and code of the StartServiceJob method. It makes the time the new section and […]

    Pingback by Creating profile properties and sections the SharePoint 2010 way – part 2, The code « Second Life of a Hungarian SharePoint Geek — March 17, 2010 @ 02:53

  3. […] So we have to wait until data is synchronized from the Social Database to the Content Database. This task is performed by the User Profile Service Application – Social Rating Synchronization Job (as its description says: Timer Job to synchronize rating values between Social Database and Content database). The job is scheduled to run hourly by default. If you can’t wait, you can start the job immediately either from the UI or from code, as described in this post. […]

    Pingback by Rate an item from code « Second Life of a Hungarian SharePoint Geek — December 6, 2010 @ 00:17


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: