Debugging SharePoint 2007 Customer Timer Jobs–The easy way

Debugging SharePoint 2007 Customer Timer Jobs–The easy way

When debugging your custom SharePoint 2007 Timer Jobs you don’t necessarily need to attach to the OWSTimer process. All the blogs and information I have found out there on the interwebs suggest two options

  1. Attach your Timer Job code to the OWSTimer process and then wait for the job to execute
  2. Create a Console Application with the below code and run it whilst your Timer Job project is attached  -

[code lang="csharp"]
using (SPSite site = new SPSite("yoursharepointsiteurl"))
{
using (SPWeb web = site.OpenWeb())
{
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Title == "Your Job Name")
{
job.Execute(Guid.Empty);
}
}

}
}
[/code]
However I have had trouble getting the second option to work. For me when I let my Console Application run, the job doesn’t seem to execute and the breakpoints in my attached code don’t get hit.

Actually the EASIEST way is to place a breakpoint on the job.Execute line and then step in to the Execute method, this will actually step in to your Timer Job code and hit your breakpoints. However in order for it to step in to the code correctly you will need to have your Timer Job project open in a separate Visual Studio window/instance.

Hope this helps!

One thought on “Debugging SharePoint 2007 Customer Timer Jobs–The easy way

  1. john

    I have created a Timer Job and deployed the wsp to the central admin. The feature is installed and activated fine. I don’t get any error. But i can’t see the timer job in the Timer job list in central admin. I dont see the feature in the site collection feature either.

    1)I did install and activate the features using STSADM commands.
    2) The Timer job does not appear under Job Definitions in sharepoint manager. The feature however does appear under the Feature definitions in sharepoint manager

    The Webapplication has three site collections and each site collection has a separate database database

    MY code is as follows
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    namespace TestTimer
    {
    public class TestTimerJob:SPJobDefinition
    {
    public TestTimerJob() : base()
    {

    }
    public TestTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
    : base(jobName, service, server, targetType)
    {
    this.Title = “Test Timer Job”;

    }
    public TestTimerJob(string jobName, SPWebApplication webApplication)
    : base(jobName, webApplication, null, SPJobLockType.Job)
    {
    this.Title = “Test Timer Job”;
    }

    public override void Execute(Guid targetInstanceId)
    {
    try
    {
    SendEmail();
    }
    catch (Exception ex)
    {
    LogError(ex.InnerException.ToString(), ex.StackTrace + ex.Source);
    }
    }
    private void SendEmail()
    {
    try
    {
    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

    //msg.To.Add(ToEmailAddress);
    msg.To.Add(“****”);
    msg.From = new System.Net.Mail.MailAddress(“”********”;”);
    msg.Subject = “Subject”;

    msg.IsBodyHtml = true;
    string EmailBody = ” Welcome to Send an Email!! Example.”;
    msg.Body = EmailBody;
    string smtp = “***”;
    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = “********”;
    NetworkCred.Password = “********”;
    NetworkCred.Domain = “********”;
    client.Credentials = NetworkCred;
    client.Send(msg);
    }
    catch (Exception ex)
    {

    LogError(ex.InnerException.ToString(), ex.StackTrace);
    }

    }

    }
    }
    Feature Reciever code below
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using System.Data.SqlClient;
    namespace TestTimer
    {
    class TestTimerReceiver : SPFeatureReceiver
    {
    const string SYNC_JOB_NAME = “My_Timer_Job”;
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
    try
    {
    SPWebApplication webapp = (SPWebApplication)properties.Feature.Parent;
    foreach (SPJobDefinition job in webapp.JobDefinitions)
    {
    if (job.Name.ToLower()==SYNC_JOB_NAME.ToLower())
    {
    job.Delete();
    }
    }

    TestTimerJob timerJob = new TestTimerJob(SYNC_JOB_NAME, webapp);
    SPMinuteSchedule schedule = new SPMinuteSchedule();
    schedule.BeginSecond = 0;
    schedule.EndSecond = 59;
    schedule.Interval = 5;
    timerJob.Schedule = schedule;
    timerJob.Update();
    }
    catch (Exception ex)
    {

    }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
    try
    {
    SPWebApplication webapp = (SPWebApplication)properties.Feature.Parent;
    foreach (SPJobDefinition job in webapp.JobDefinitions)
    {
    if (job.Name.ToLower()==SYNC_JOB_NAME.ToLower())
    {
    job.Delete();
    }
    }
    }
    catch (Exception ex)
    {

    }
    }

    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {

    }

    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {

    }
    }
    }
    and Feature .xml below

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">