Create a Timer Job in SharePoint 2013
Step by Step procedures to create a Timer Job in SharePoint 2013
Description:
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 :
- Derive CustomTimerJob Class from SPJobDefinition
- Add the three Constructors of the derived class : When ever we create the object of CustomTimerJob class, corresponding constructor will execute.
- Override the Execute method: When ever the timer job start running then the code inside the Execute method will run.
- 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:
- 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
- Click on Office/SharePoint
- Click on SharePoint solutions
- 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.
- Please give a name of project in new project window, in my context project name: customtimerjob
- Click on Ok Button in the new project window
Step 4:
SharePoint Customization Wizard
- Provide the SharePoint Site url where you want deploy the project solution (i.e customtimerjob project ). In my contenxt : http://c4968397007
- Just cross check does the site is available to deploy the project solution or not by just click on Validate button.
- 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:
- Right Click on project and select add new item then add folder to the project solution ( this is for just convenient but not mandatory )
- In this folder i will add customtimerjob class of the project.
Step 7:
- Rename the folder, in my context it's SPJobs
Step 8:
- Right click on SPJobs folder and select add new class
- Name it's customtimerjob.cs
Step 9:
Step 10:
- Extend the customtimerjob class from SPJobDefinition.
- Add the three default constructors of SPJobDefinition class to cutomtimerjob.
- Over ride the Execute method in customtimerjob class.
- Our main code we will keep it in execute method of the customtimerjob.
Add the following code:
Registering the Timer Job Definition:
Step 11:
- Right click on the Features folder of the project and select the option " add a feature "
Step 12:
- Rename the above added feature to SPTimerJobFeature1 ( The name is up to you convenience)
Step 13:
- 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:
- Change the Active on Default to False so that the customtimerjob will not active on the all web application by default.
- So what ever the Web Application we want to active the timer job, we will do it manually through the central admin.
Step 15:
- Now we are adding the feature receivers to feature.
- Right click on the SPTimerJobFeature1 and select the add event receiver option
Step 16:
- 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
- FeatureActivated
- FeatureDeactivating
- FeatureInstalled
- FeatureUninstalling
- FeatureUpgrading
Step 18:
- In the FeauteActivated method i added code
- In the Line No : 5 i used SPSecurity.RunWithElevatedPrivileges for getting admin privileges for this particular task
- In the Line No : 7 & 8 i create objects for the web application and site
- 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.
- 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:
- It will build a one .dll (Dynamic Link Library) and one .wsp ( Windows Sharepoint ) file for the package.
- Recycle ISS Application Pool : Skipped because it's first we are deploying the package or project solution.
- Retract Solution: Skipped because it's first we are deploying the package
- Add Solution: Done
- Deploy Solution: Done
- 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 :
- Open the Central Admin.
- In the Application Management click on Manage Web Applications.
Step 22:
- Select the web application.
- Click on Manage features at top right corner of the web page.
Step 23:
- You will get a pop window show all the features are associalted with web application.
- 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