Then, make sure you create this project as a Farm Solution.

Now, add a Class to the Project. Name it “ListDescTimer.cs”. Your Solution Explorer should now look something like this:

The class within this cs file should inherit from SPJobDefinition. It also needs 3 methods, 2 constructors, including a default constructor, and an execute method. In the end, at the very least, you need this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace ExampleTimerService
{
class ListDescTimer : SPJobDefinition
{
// Default constructor
public ListDescTimer()
{
}
// Constructor with a controlled name.
// This will be handy on deactivate
public ListDescTimer(SPWebApplication webApp, string JOB_NAME)
: base(JOB_NAME, webApp, null, SPJobLockType.ContentDatabase)
{
this.Title = JOB_NAME;
}
// This is what happens when the timer service executes.
public override void Execute(Guid targetInstanceId)
{
}
}
}
Okay, I’d we are already 1/3 of the way there. Now we need to add a SharePoint Feature to actually deploy and remove the timer service. This feature will need to be scoped at a web application. That’s where all the timer services run.
So, let’s add a feature:


We will also use a receiver to make sure the timer service (ListDescTimer.cs) is properly added or removed:

Inside the EventReceiver.cs code, we need two methods as shown here:
FeatureActivated should remove all older copies of the service, and install a new one. The FeatureDeactivated method should only remove all copies of this timer service. I also use a constant to keep track of the service name.
public class Feature1EventReceiver : SPFeatureReceiver
{
private const string JOB_NAME = "Timer - List Description";
public override void FeatureActivated
(SPFeatureReceiverProperties properties)
{
}
public override void FeatureDeactivating
(SPFeatureReceiverProperties properties)
{
}
}
The code for these two method generally look something like this:
public class Feature1EventReceiver : SPFeatureReceiver
{
private const string JOB_NAME = "Timer - List Description";
public override void FeatureActivated
(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == JOB_NAME)
job.Delete();
}
ListDescTimer timerService = new ListDescTimer(webApp, JOB_NAME);
timerService.Schedule = SPSchedule.FromString("every 5 minutes");
timerService.Update();
}
public override void FeatureDeactivating
(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == JOB_NAME)
job.Delete();
}
}
}
This feature adds and removes the service to the web application, based on a constant name. Now, we are 2/3 of the way there. The only step left is to actually implement the execute method in the Timer Service. Again, we want to find a particular list, and change its description.
// This is what happens when the timer service executes.
public override void Execute(Guid targetInstanceId)
{
SPSite site = this.WebApplication.Sites["Examples"];
// "Examples" is the root site's display name.
using (SPWeb web = site.OpenWeb("/"))
{
SPList list = web.Lists["Custom List 001"];
int count = list.ItemCount;
list.Description =
"There are " + count + " items here.";
list.Update();
}
}
This completes our brief coding. Now we simply deploy it using the Visual Studio 2010 Interface:

Now to activate this feature, we would normally have to go to central administration. In central administration, we would follow the “Manage web applications” link to the top left:

Then we would select the correct web application, got to Manage Features, and activate the correct features.

HOWEVER, since we deployed it through visual studio, this is already deployed and activated. You can check the status of your timer service by going to Monitoring:

Under Timer Jobs, select Review Job Definitions to find your deployed timer services. The timer services can also be filtered based on a several parameters, including web application. You can also force the timer service to execute on-the-spot:

Anyway, the result of the previously mentioned code is this:

I hope this helps you create a timer service with confidence. Good luck!
By: Arjun Chakraborty