JG Vimalan's Blog

It is all about coding!

Create a custom timer service in MOSS 2007

To create a simple timer service, just few thigs needs to be considered.

Create a class library which contains two class (in general).

1. Manager.cs    – Inherits from SPJobDefinition and overrides the Execute(Guid targetInstanceId) method.

2. ServiceController – To create / delete the service

Create a test app (console is enough) for calling the create / delete a service methods

The code snippets are given below and i hope it is easy to understand.

Manager.cs class

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Diagnostics;

namespace SampleTimerService
{
    public class Manager : SPJobDefinition
    {
        #region Constructors

        /// <summary>
        /// Default
        /// </summary>
        public Manager()
            : base()
        { }

        
        public Manager(string mail, SPService service, SPServer server, SPJobLockType targetType)
            : base(mail, service, server, targetType)
        { }

        
        public Manager(string mail, SPWebApplication webApplication)
            : base(mail, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = “Custom Manager”; //Service Name. Can be viewed in

                                                         //Central Admin Page
        }

        #endregion Constructors 

       /// <summary>
        /// This method will be executed by the SharePoint timer in the specified time

       ///interval.
        /// Also, this method can be invoked from outsite by just referencing this

        ///assembly.
        /// </summary>
        /// <param name=”targetInstanceId”></param>
        public override void Execute(Guid targetInstanceId)
        {

                 //Write your business logic here

         } 

ServiceController.cs

using System;
using System.Text;
using System.Collections;
using Microsoft.SharePoint;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

 namespace SampleTimerService
{
    /// <summary>
    ///
    /// </summary>
    public class ServiceController
    {
        /// <summary>
        ///
        /// </summary>
        public static void CreateService()
        {
            SPServer server = SPServer.Local;
            SPSite site = new SPSite(ConfigurationManager.AppSettings[“URL”]);
            SPWebApplication web = site.WebApplication;
            SPJobDefinitionCollection jobs = web.JobDefinitions;

            SPSchedule schedule = SPSchedule.FromString(“daily at 18:05:00”);
           
            Manager manager = new Manager(“email”, web);
            manager.Schedule = schedule;
            jobs.Add(manager);

            web.Update();
            site.Dispose();
        }

        /// <summary>
        ///
        /// </summary>
        public static void DeleteService()
        {
            Guid id = new Guid();
            SPSite site = new SPSite(ConfigurationManager.AppSettings[“URL”]);
            SPWebApplication web = site.WebApplication;
            SPJobDefinitionCollection jobs = web.JobDefinitions;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Title == “Custom Manager”)
                {
                    id = job.Id;
                    break;
                }
            }

            if (id != null)
            {
                jobs.Remove(id);
                web.Update();               
            }

            site.Dispose();
        }
    }
}

Other steps to follow before creating the timer service:

1. Provide strong name to the assemble.

2. Add the assemply in the GAC.

3. Put the assembly in the 80/bin folder

Finally the test application (console) to create / delete a service

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

namespace ServiceManager
{
    class Program
    {
        static void Main(string[] args)
        {
            LeaveManager.ServiceController.DeleteService();
            LeaveManager.ServiceController.CreateService();
        }
    }

Once the service is created, it can be activated/deactivate from the central admin page.

Advertisements

September 27, 2009 - Posted by | SharePoint

No comments yet.

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

%d bloggers like this: