Timer Job in SharePoint 2013

Creating Custom Timer Job in SharePoint 2013:

Timer Job is a background process which is periodically executed inside SharePoint Server.

Using Timer Job we can execute tasks like

  • sending emails every day
  • updating list data every day
  • delete unused sites
  • user profile cleanup

Components in creating Custom Timer Job:

  • Derive this CustomTimerJob from SPJobDefinition class i.e. public class CustomTimerJob : SPJobDefinition
  • Add the three default constructors of SPJobDefinition class to cutomtimerjob
  • 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.

Create Custom Timer Job using Visual Studio:
Step 1 :
*Create an empty SharePoint project[customtimerjob] and Add a new class to the project[SPTimerJobClass] and inherit the base class SPJobDefinition
*Add the three default constructors of SPJobDefinition class to cutomtimerjob.
*Override the Execute method in customtimerjob class.
*write your code in execute method of the customtimerjob.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;namespace customtimerjob.SPJobs
{
class SPTImerJobClass : SPJobDefinition
{
public SPTImerJobClass() : base() { }public SPTImerJobClass(string jobName, SPService service): base(jobName, service, null, SPJobLockType.None)
{
this.Title = “Task Complete Timer”;
}public SPTImerJobClass(string jobName, SPWebApplication webapp): base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = “Task Complete Timer”;
}public override void Execute(Guid targetInstanceId)
{
SPWebApplication webapp = this.Parent as SPWebApplication;
SPList tasklist = webapp.Sites[0].RootWeb.Lists[“Tasks”];
SPListItem newTask = tasklist.Items.Add();
newTask[“Title”] = “New Task” + DateTime.Now.ToString();
newTask.Update();
}}
}
  • Now the TimerJob is ready and we need to create a new feature and a feature receiver.
  • On the activation of the feature, we are going to add this timer job into our farm.
  • Add a feature by right click on feature folder in your Visual Studio Project and click on Add Feature Name the feature and other properties.
On Feature activated event, write the code
Public override void FeatureActivated(SpFeature receiverProperties properties)
{
try
{
MyTimer timer=new MyTimer(“Name”,”url”,….);
SpDailySchedule schedule=new SpDailySchedule();
schedule.BeginHours=01;
schedule.BeginMinute=0;
schedule.EndHours=…;
timer.schedule=schedule;
timer.update();
}
catch(Exception EX)
{
}

}To Debug this code, we need to attach the debugger to “OWSTimer.exe” process.

Source:
http://www.codeproject.com/Tips/634208/Create-and-Deploy-Custom-Timer-Job-Definition-in-S
http://www.sharepointpals.com/post/Step-by-Step-procedures-to-create-a-Timer-Job-in-SharePoint-2013
http://www.splessons.com/2013/12/create-a-timer-job-in-sharepoint-2013/

 

Advertisements