0

I have the following problem when trying to create a timer job

I created my job based on this article: http://msdn.microsoft.com/en-us/library/hh528519.aspx

However, debugging the feature activated event, I got a Sequence Contains No Elements exception on this code:

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 == "SynchronizationUserAccessListJob"
                          select s;

            SPService service = results.First();

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

            // Create the job.
            SynchronizationUserAccessListJob job = new SynchronizationUserAccessListJob(
                "SynchronizationUserAccessListJob" , 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 = 5;
            job.Update();            
        }

When I reviewed the farm.Services collection in the debugger, in fact my new timer job is not there.

To install it I: 1. Add-SPSolution 2. Install-SPSolution 3. Enable the feature on central administration farm feature.

What else am I missing??

If somebody ask me I would say the code provided in that article is wrong, It should check first if it the linq query returns result, if Not, then it should create an instance of the object

Right or wrong?

2 Answers 2

0

Use FirstOrDefault instead of First:

SPService service = results.FirstOrDefault();

if (service != null){
    DeleteJob(service);
}
2
  • lol I already did that before you answered... lazy to read their code :) Mar 30, 2012 at 14:12
  • it looks like this code is correct if you use FirstOrDefault funstion and check service object for null value
    – Alexander
    Mar 30, 2012 at 14:16
0

Looks like you need to add:

job.Schedule = schedule;
job.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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