Results 1 to 3 of 3

Thread: Sharepoint (MOSS) timer jobs and the timer service

  1. #1
    Join Date
    Nov 2008
    Posts
    1,514

    Sharepoint (MOSS) timer jobs and the timer service

    In the development of customized solutions based on Windows Sharepoint Services, I've very often have to create console application (executable for instance) for the systematic execution of operations according to certain time intervals. Before the advent of Office SharePoint Server 2007, creating an executable was the only way to perform scheduled operations.
    Now, Microsoft has seen fit to introduce the "timer jobs", the particular object classes whose instances are executed by a process of Sharepoint, in time for their installation. The service is responsible for performing these tasks, it is named "Windows SharePoint Timer Service" (SPTimerV3).
    The entire system itself Sharepoint, using these objects for its own purposes:
    • Database of statistics and reports
    • Alert Mechanism
    • Cleanup Sites
    • Profile Synchronization
    • Basket
    • and many others ...

    The list of these jobs can be viewed from the Central Administration, following the route Operations> Timer Jobs Definitions; on this page you can see the names of various jobs, the Web Application in which they are registered and the type of schedule. Instead of going on the "Timer Job Status, you can enable or disable specific job, see the implementation rate, the final result is available (to check that they are not finished with errors), the server running and the date of last run.

  2. #2
    Join Date
    May 2008
    Posts
    1,020

    Re: Sharepoint (MOSS) timer jobs and the timer service

    Clearly, as all the new features in this latest version of the product, the system timer job can be extended through the creation of their job.
    What to do? Nothing could be easier, just create the usual class library. NET, marked by a key and add a new class that inherits from the class SPJobDefinition (in this namespace Microsoft.SharePoint.Administration). This class requires the definition of three builders, and a method, the Execute method, which will contain the code. NET that we want to execute the predetermined intervals.

    In the example I decided to offer, I created a timer job to control when a workflow (specifically un'approval workflow) has come to an end. This was a real need, because there is no event that allows us to determine when the workflow is completed or not (even ItemUpdated the event of a list, because it is not triggered).
    Code:
      using System;
    
      using System.Collections.Generic;
    
      using System.Text;
    
      using Microsoft.SharePoint.Administration;
    
      using Microsoft.SharePoint;
    
    
    
      namespace Peppe.Sharepoint.Jobs
    
      {
    
      public class WorkflowMonitor: SPJobDefinition
    
      {
    
      public WorkflowMonitor (): base () {}
    
    
    
      public WorkflowMonitor (string jobName, SPService service,
    
      SPServer server, targetType SPJobLockType)
    
      : Base (jobName, service, server, targetType) {}
    
    
    
      public WorkflowMonitor (string jobName, SPWebApplication WebApplication)
    
      : Base (jobName, WebApplication, null, SPJobLockType.ContentDatabase)
    
      {
    
      this. Title = "Approval Workflow Monitor";
    
      }
    
    
    
      public override void Execute (Guid targetInstanceId)
    
      {
    
      SPSite site = new SPSite ("http://localhost");
    
      using (site)
    
      {
    
      SPWeb web = site.OpenWeb ();
    
      using (web)
    
      {
    
      Web.Lists SPList list = ["listname"];
    
      SPListItemCollection items = list.Items;
    
    
    
      foreach (SPListItem item in items)
    
      {
    
      if (item.Workflows [0]. IsCompleted)
    
      {
    
      / / WORKFLOW COMPLETED! 
    
                             }
    
    
    
      if (item ["FieldName"]! = null)
    
      {
    
      if (item ["FieldName"]. ToString () == "16")
    
      {
    
      / / ITEM APPROVED 
    
                                 }
    
      if (item ["FieldName"]. ToString () == "17")
    
      {
    
      / / ITEM REFUSED 
    
                                 }
    
      }
    
      }
    
      }
    
      }
    
      }
    
      }
    
      }

  3. #3
    Join Date
    Nov 2009
    Posts
    687

    Re: Sharepoint (MOSS) timer jobs and the timer service

    Installing the Job

    Once you create the library containing the job of our example, we have to worry about installation. Unfortunately, for some types of event handlers, the only way to register a job within Sharepoint is by writing a few lines of code, and these can be placed either within a receiver of a feature (for this refer to the reading of ' article on the event handler ), both within a console application.

    For this example I chose to develop a console application. We must add to the current solution to Visual Studio. NET and include in the reference the class library containing our job. In the main application, we then create a new instance of our job and a new instance of the class SPMinuteSchedule class that allows us to set the values of execution time of our job, specified in minutes, or you can use the class SPDailySchedule, in order to run the job every few days.

    Note: Each definition of a single job, must be associated to a Sharepoint Web Application and that is why we must create an instance of our own, using the constructor that takes as parameters the name of the job and its web application. Without this, we write the lines needed for stopping and restarting the SharePoint Timer Service (SPTimerV3 service), so as to make it aware of the presence of our job or a new version.
    Code:
      namespace InstallJob
    
      {
    
      class Program
    
      {
    
      const string NOMEJOB = "WorkflowMonitor";
    
    
    
      static void Main (string [] args)
    
      {
    
      InstallJob ();
    
    
    
      Console.WriteLine ("Job successfully installed");
    
      Console.Read ();
    
      }
    
    
    
      private static void InstallJob ()
    
      {
    
      SPSite site = new SPSite ("http://localhost");
    
      using (site)
    
      {
    
      / / Delete the definition of the job 
    
                     foreach (j in SPJobDefinition site.WebApplication.JobDefinitions)
    
      {
    
      if (j.Name == NOMEJOB)
    
      j.Delete ();
    
      }
    
    
    
      / / Instance of the job 
    
                     WorkflowMonitor job = new WorkflowMonitor (NOMEJOB, site.WebApplication);
    
      SPMinuteSchedule SPMinuteSchedule = new schedule ();
    
      schedule.BeginSecond = 0;
    
      schedule.EndSecond = 59;
    
      schedule.Interval = 1;
    
      job.Schedule = schedule;
    
      job.Update ();
    
    
    
      / / Service "Windows Sharepoint Services Timer" 
    
                     ServiceController controller = new ServiceController ();
    
      controller.MachineName = "."
    
      controller.ServiceName = "SPTimerV3";
    
    
    
      / / Stop the service 
    
                     controller.Stop ();
    
      / / Wait until the stop 
    
                     Thread.sleep (3000);
    
      / / Start the service 
    
                     controller.Start ();
    
      }
    
      }
    
      }
    
      }

Similar Threads

  1. Add Timer in c#
    By JasonWung in forum Software Development
    Replies: 1
    Last Post: 18-04-2012, 06:21 PM
  2. Timer Service support for Web Services in EJB 2.1
    By Mei-Xiu in forum Software Development
    Replies: 7
    Last Post: 08-09-2010, 10:03 PM
  3. Scheduling in EJB3 Timer Service
    By Cheng-Gong in forum Software Development
    Replies: 5
    Last Post: 24-07-2010, 06:06 AM
  4. Execute timer service in java
    By Messenger in forum Software Development
    Replies: 4
    Last Post: 20-07-2010, 01:27 PM
  5. how timer work in c#
    By Visala28 in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 11:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,520,226.74731 seconds with 17 queries