Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a very simple question on SharePoint 2010 Timer Job, I build by reference from here.

Basically upon execute of the Timer Job, I add an entry to SharePoint Custom List as following:

public override void Execute(Guid targetInstanceId)
{
    myFunction();
}

public void myFunction()
{
 using (SPSite site = new SPSite("http://MySPSite/sites/dev"))
 {
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["MyList"];

        web.AllowUnsafeUpdates = true;

        SPListItem item = list.Items.Add();
        item["Title"] = "value A";
        item.Update();

        web.AllowUnsafeUpdates = false;
    }
 }
}

I build and deploy the project into SharePoint site, it works. However when I change the value to "value B" let say, then build and deploy, when the Timer Job execute, it is still adding "value A" into my Custom List.

I've try debug by attached to owstimer.exe process,it show when the code execute, it is previous value. Why is the previous value still retain? any best way to remove it? else it is very difficult for me to debug in Timer Job development.

Thank you in advanced.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Make sure you reset the Timer service for SharePoint after your deploy to force it to pick up your updates:

In PowerShell:

net stop SPTimerV4
net start SPTimerV4
share|improve this answer

Make sure you delete the job in your feature activation in addition to stopping and starting the timer job. According to Microsoft documentation you must delete the job in your feature deactivation before it is re-added.

http://msdn.microsoft.com/en-us/library/hh528519(v=office.14).aspx

C# public override void FeatureActivated( SPFeatureReceiverProperties properties) {

// Get an instance of the SharePoint farm.
SPFarm farm = SPFarm.Local;

// Get an instance of the service.
var results = from s in farm.Services
              where s.Name == "SPSearch4"
              select s;

SPService service = results.First();

// Remove job if it exists.
DeleteJobAndSettings(service);

// Create the job.
MonitoringJob job = new MonitoringJob(
    MonitoringJob.JobName, service);

// Create the schedule so that the job runs hourly, sometime 
// during the first quarter of the hour.
SPHourlySchedule schedule = new SPHourlySchedule();
schedule.BeginMinute = 0;
schedule.EndMinute = 15;
job.Update();

// Configure the job.
MonitoringJobSettings jobSettings = new MonitoringJobSettings(
    service, Guid.NewGuid());
jobSettings.EmailAddress = "myemail@demo.com";
jobSettings.Update(true);

}

public override void FeatureDeactivating( SPFeatureReceiverProperties properties) {

// Get an instance of the SharePoint farm.
SPFarm farm = SPFarm.Local;

// Get an instance of the service.
var results = from s in farm.Services
              where s.Name == "SPSearch4"
              select s;

SPService service = results.First();

DeleteJobAndSettings(service);

}

private void DeleteJobAndSettings(SPService service) {

// Find the job and delete it.
foreach (SPJobDefinition job in service.JobDefinitions) {
    if (job.Name == MonitoringJob.JobName) {
        job.Delete();
        break;
    }
}

// Delete the job's settings.
MonitoringJobSettings jobSettings =
    service.GetChild<MonitoringJobSettings>(
        MonitoringJobSettings.SettingsName);
if (jobSettings != null) {
    jobSettings.Delete();
}

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.