SharePoint 2013 - SPLessons

Create a Timer Job in SharePoint 2013

SharePoint 2013 - SPLessons

Step by Step procedures to create a Timer Job in SharePoint 2013


In this article, we will see how to create a Timer Job step by step in SharePoint 2013.  Today I am going to create a timer job that will execute ever 15 minutes and creates task in task list of the SharePoint site which ever the site we activated the timer job feature.

What is a Timer Job?

A Timer Job is a periodically executed task inside SharePoint Server. It provides us a task execution environment. For example, we can execute tasks like: sending emails every hour, data updating every day, creating reports every week, etc.


From the above Figure: The Timer job will call execute method, inside of the execute method our main code snippet will create a task item in sharepoint task list.

Default Timer Jobs inside SharePoint:

There are many timer jobs inside SharePoint which do internal tasks like:

    • Send emails
    • Validate sites
    • Delete unused sites
    • Health analysis
    • Product versioning
    • Diagnostics

These tasks will having execution periods like:

    • Minute
    • Hour
    • Day
    • Week
    • Month

Components of Connectable web parts : 

  1. Derive CustomTimerJob Class from SPJobDefinition
  2. Add the three Constructors of the derived class : When ever we create the object of CustomTimerJob  class, corresponding constructor will execute.
  3. Override the Execute method: When ever the timer job start running then the code inside the Execute method will run.
  4. We need to create a Feature and Feature Receiver so on activation of this feature we are going the add our timer job to SharePoint farm.

Here is the following steps : 

Step 1: 

Click on create new project in start page of  Visual Studio 2013


Step 2:

Or You can create a new project by click on File -> New Project from top menu of Visual Studio 2013


Step 3:

  1. Chose the required template for our sharepoint projet. Click on Visual C#  because i used C# for my solution, if you want with Visual Basic you can select VB template
  2. Click on Office/SharePoint
  3. Click on SharePoint solutions
  4. Please select the .Net Framework  is : 4.5 from top menu of new project window. We can also select below 4.5 version because SharePoint 2013 is backward comparability.
  5. Please give a name of project in new project window, in my context project name: customtimerjob
  6. Click on Ok Button in the new project window


Step 4:

SharePoint Customization Wizard

  1. Provide the SharePoint Site url where you want deploy the project solution (i.e customtimerjob project ). In my contenxt : http://c4968397007
  2. Just cross check does the site is available to deploy the project solution or not  by just click on Validate button.
  3. Select the what is the trust level of SharePoint Solutions ? Select the farm solution.  You must select the Deploy as a farm solution radio button because timer jobs require a higher level of trust to execute than sandboxed solutions.

Step 5:


Step 6:

  1. Right Click on project and select add new item then add folder to the project solution ( this is for just convenient but not mandatory )
  2. In this folder i will add customtimerjob class of the project.


Step 7:

  1. Rename the folder, in my context it’s SPJobs


Step 8:

  1. Right click on SPJobs folder and select add new class
  2. Name it’s customtimerjob.cs


Step 9:


Step 10:

  1. Extend the customtimerjob class from SPJobDefinition.
  2. Add the three default constructors of SPJobDefinition class to cutomtimerjob.
  3. Over ride the Execute method in customtimerjob class.
  4. Our main code we will keep it in execute method of the customtimerjob.

Add the following code:



Registering the Timer Job Definition:

Step 11:

    1. Right click on the Features folder of the project and select the option ” add a feature “



Step 12:

    1. Rename the above added feature to SPTimerJobFeature1 ( The name is up to you convenience) 



Step 13:

    1. Please select the scope of the feature as WebApplication so the solution will gain more control over all the site collection in web application, you want with differient scope you can chose based on your requirement.



Step 14:

    1. Change the Active on Default to False so that the customtimerjob will not active on the all web application by default.
    2. So what ever the Web Application we want to active the timer job, we will do it manually through the central admin.




Step 15:

    1. Now we are adding the feature receivers to feature.
    2. Right click on the SPTimerJobFeature1 and select the add event receiver option




Step 16:

    1. if you observer here the SPTimerjobFeature1EventReceiver class extends the SPFeatureReceiver class



Step 17:

By defualt all the Methods are commented out, un-comment the methods that you want and add the code inside the method

    1. FeatureActivated
    2. FeatureDeactivating
    3. FeatureInstalled
    4. FeatureUninstalling
    5. FeatureUpgrading




Step 18:


  1. In the FeauteActivated method i added code
  2. In the Line No : 5  i used SPSecurity.RunWithElevatedPrivileges for getting admin privileges for this particular task
  3. In the Line No : 7 & 8 i create objects for the web application and site
  4. In Line No: 8 i called a method DeleteExistingJob(JobName, parentWebApp); this is a user defined method. In this method will check the whether any job exist in SharePoint Farm with JobName  if so this method i will delete the particular job because to maintain consistency.
  5. In Line No: 9 i called a method CreateJob(parentWebApp); this is a user defined method. In this method will create a SPTImerJobClass object and attached to SPMinuteSchedule so that on Every 15 minutes the SPMinuteScheduler will call Execute method of our customTimer Job:


Add the following code in  

Step 19:

We are ready to deploy our solutions just right click on the project select the deploy option so the visual studio do the reaming background steps

BackGround Steps are mentioned in the step 20


Step 20:

BackGround Step happenes once we pass the deploy command to visual studio:

  1. It will build a one .dll (Dynamic Link Library)  and one .wsp ( Windows Sharepoint ) file for the package.
  2. Recycle ISS Application Pool : Skipped because it’s first we are deploying the package or project solution.
  3. Retract Solution: Skipped because it’s first we are deploying the package
  4. Add Solution: Done
  5. Deploy Solution: Done
  6. Activate Features: Skipped because we  have given an option Active Default to False in the step 14 so we need to active the feature manually.


Step 21:

Now we are going to active our customtimerjob feature :

  1. Open the Central Admin.
  2. In the Application Management click  on Manage Web Applications.


Step 22:

  1. Select the web application.
  2. Click on Manage features at top right corner of the web page.


Step 23:

  1. You will get a pop window show all the features are associalted with web application.
  2. Here we will find our customtimerjob named as SPTimerJobFeature1 and click on Active that’s it.


Step 24:


Step 25:

If you see here the timer job has been created a item in the task list of SharePoint Site