Wednesday, February 22, 2012

custom timer job sharepoint 2010

Here we will see how we can create a custom timer job using visual studio 2010 for our SharePoint site. Here our first step is to create a empty SharePoint project. For this open visual studio 2010 then File -> New -> Project. From the New Project dialog box
select SharePoint -> 2010 and then select Empty SharePoint project as shown in the figure below. Give a name and click on OK.
In the next step give the local deployment url and select farm solution and click on Finish.
Then in the next step we need to add one class. So right click on the project -> Add New Item then from the Add New Item dialog box select Visual C# and select class and then give a name to the class and click on Add as shown in the figure below.
Now open the class that we have just created and add the below 2 namespace:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


Now to work with custom timer jobs the class should be derived from SPJobDefinition class, so do the changes to the class below:

public class OurTimerJobClass : SPJobDefinition

To work with timer jobs we need to we need to override some constructors and also need to override the Execute() method.

So add below codes for constructors.

public OurTimerJobClass() : base() { }

 public OurTimerJobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)
   : base (jobName, service, server, targetType) {}

Now we need to override the Execute method which is the method where we need to write our code that our event receiver will do.

 public override void Execute(Guid targetInstanceId)
        {
            // Your logic will go here!!!
        }

So over all our class should looks like this:

public class OurTimerJobClass : SPJobDefinition
    {
        public OurTimerJobClass() : base() { }

        public OurTimerJobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)
   : base (jobName, service, server, targetType) {}

        public override void Execute(Guid targetInstanceId)
        {
            // Your logic will go here!!!
        }
    }

This finishes the creation of custom timer job, we will discuss about deployment in different article in a separate post.

No comments:

Post a Comment