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 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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use FirstOrDefault instead of First:

SPService service = results.FirstOrDefault();

if (service != null){
    DeleteJob(service);
}
share|improve this answer
    
lol I already did that before you answered... lazy to read their code :) –  L.V. Sharepoint Architect 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

 
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.