SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I created my job based on this article:

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.

            // 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;

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?

share|improve this question
up vote 0 down vote accepted

Use FirstOrDefault instead of First:

SPService service = results.FirstOrDefault();

if (service != null){
share|improve this answer
lol I already did that before you answered... lazy to read their code :) – Esteban V Mar 30 '12 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 '12 at 14:16

Your Answer


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.