2

I have a TimerJob in my feature that is not executing.

I've tried restarting the timer service (below), deactivating and activating the feature, and manually executing the job using the "run now" button in central administration. Nothing will make the Execute method execute.

net stop sptimerv4
net start sptimerv4

Here's the feature activation code:

const string JOB_NAME = "CampaignGenerationTimerJob";
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb oWebsite = properties.Feature.Parent as SPWeb)
        {
            var site = oWebsite.Site;
            var jobs = site.WebApplication.JobDefinitions.Where(x => x.Name == JOB_NAME);
            foreach (var job in jobs)
            {
                job.Delete();
            }
            var campaignGenerationTimerJob = new CampaignGenerationTimerJob(JOB_NAME, site.WebApplication);
            var schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;
            campaignGenerationTimerJob.Schedule = schedule;
            campaignGenerationTimerJob.Update();
            campaignGenerationTimerJob.RunNow();
        }
    }

And then I have my Timer job:

 class CampaignGenerationTimerJob : SPJobDefinition
{
    public CampaignGenerationTimerJob()

        : base()
    {

    }

    public CampaignGenerationTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)

        : base(jobName, service, server, targetType)
    {

    }

    public CampaignGenerationTimerJob(string jobName, SPWebApplication webApplication)

        : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
    {

        this.Name = "CampaignGenerationTimerJob";
        this.Title = "CampaignGenerationTimerJob";

    }

    public override void Execute(Guid contentDbId)
    {
        SPWebApplication webApplication = this.Parent as SPWebApplication;

        SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

        using (var data = new DataContext(SPContext.Current.Web.Url))
        {
            var campaign = data.GetList<CampaignsListFieldsContentType>("Campaigns").First();
            campaign.Title = "Updated the first item title";

            data.SubmitChanges();
        }

    }
}
3

Have you tried viewing logs with ULS viewer?

I think your problem might be that you are using SPContext.Current in a timer job.

  • I didn't check the logs, but just as a quick test, I replaced the SPContext.Current.Web.Url with the hard-coded url, and it worked. Do you know an alternative to get the relative site URL from the timer job? – xdumaine Nov 4 '13 at 18:17
  • And as a side note, is it possible to debug this execute method? – xdumaine Nov 4 '13 at 18:21
  • I believe it is possible to retrieve site url trough SPWebApplication.Sites. Might be a better way but don't know now. As for debugging, yes it is possible with Visual Studio, you need to attach to OWSTIMER process and then run the timer job. Debugging timer jobs - msdn.microsoft.com/en-us/library/ff798310.aspx – Rae23 Nov 4 '13 at 18:26
3

You should get site url as below.

string siteUrl = this.Properties["SiteURL"].ToString();

and use below code to get current web in timerjob.

        using (SPSite site = new SPSite(siteUrl))
        {
                using (SPWeb CurrentWeb = site.OpenWeb())
                {

                   // Write code here

                }

        }
1

SPContext is unavailable in timer jobs because timer jobs are executing at SPWebApplication scope, not SPSite or SPWeb. You can access that property using this.WebApplication.Sites[..].Url object

The only way you can get a current site in a timerjob is when you are itterating the this.WebApplication.Sites collection to execute some SPSite scope code.

SPContext not available to following

  • Console Application/Windows application running on your server!!
  • Timer Jobs
  • Event Receivers

Solution

using(SPSite oSite = new SPSite(this.WebApplication.Sites[0].Url))
{
  using(SPWeb oWeb = oSite.OpenWeb())    
  {

  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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