In this article I’m going to
explain about creation of custom Timer Jobs in SharePoint 2013 with step by
step. Today I am going to create a timer job that will execute
every 5 minutes and creates task in task list of the SharePoint site whichever
the site we activated the timer job feature.
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.
By default some Timer jobs are in
SharePoint Web Application. Those are,
ü Validate sites
ü Delete unused sites
ü Health analysis
üProduct versioning
üDiagnostics
These
tasks will have execution periods like,
ü Minute
ü Hour
ü Day
ü Week
ü Month
ü Hour
ü Day
ü Week
ü Month
Step1:-
Step2:-
ü Select
language as visual c#.
ü Click on “Office/SharePoint”.
ü Select “SharePoint2013-Empty Project”.
ü Give name as “SPCustomTimerJob”
ü Click on “OK” to create project.
ü Click on “Office/SharePoint”.
ü Select “SharePoint2013-Empty Project”.
ü Give name as “SPCustomTimerJob”
ü Click on “OK” to create project.
Step3:-
ü Provide the
site URL to the project where you want deploy the solution.
ü Click on Validate button to check the connectivity.
ü Select the trusted level to deploy solution check the “Deploy as Farm Solution” because it has highly trusted than Sandbox solution.
ü Click on “Finish”.
ü Click on Validate button to check the connectivity.
ü Select the trusted level to deploy solution check the “Deploy as Farm Solution” because it has highly trusted than Sandbox solution.
ü Click on “Finish”.
Step4:-
Step5:-
Step6:-
Step7:-
ü Inherit the SPJobDefinition class from SPCustomTimerJob.
ü Add the three
default constructors of SPJobDefinition class to SPCustomTimerJob.
ü Over ride
the Execute method in SPCustomTimerJob class.
ü Our main code we
will keep it in Execute method of the SPCustomTimerJob.
Step8:-
Step9:-
ü Select the
scope of the Timer job as “WebApplication”.
Step10:-
ü Right click
on the “SPTimerJobFeature” and select “Add Event Receiver”.
ü Name it as
SPTimer.
Step12:-
If
you observer here the SPTimerjobFeature1EventReceiver class extends the SPFeatureReceiver class.
By default all the
Methods are commented out, un-comment the methods that you want and add the
code inside the method
ü
FeatureActivated
ü
FeatureDeactivating
ü
FeatureInstalled
ü
FeatureUninstalling
Step13:-
ü In
FeatureActivated method i used SPSecurity.RunWithElevatePrivileges.
ü And
instantiated the WebApplication and site.
Step14:-
ü Right
click on project name and click on “Deploy” to deploy the solution into the
site.
Step15:-
ü Go to the
Central Administration.
Step16:-
ü Select the
web application in which the solution is deployed.
Step17:-
ü You can find
the Feature whatever implemented in visual studio.
Step18:-
ü Then the
feature activated as shown in below figure.
Step19:-
ü Now
go to the list to check whether the task is created or not in below shown
figure.
TimerJobClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SPCustomTimerJob.SPJobs
{
class TimerJobClass:SPJobDefinition
{
public TimerJobClass() : base() { }
public TimerJobClass(string JobName, SPService Service) : base(JobName, Service, null, SPJobLockType.None)
{
this.Title = "Timer Job
Task complete";
}
public TimerJobClass(string JobName, SPWebApplication web):base(JobName,web,null,SPJobLockType.ContentDatabase)
{
this.Title = "Timer Job
Task complete";
}
public override void Execute(Guid targetInstanceId)
{
//base.Execute(targetInstanceId);
SPWebApplication webApp = this.Parent as SPWebApplication;
SPList taskList = webApp.Sites[0].RootWeb.Lists["TaskList"];
SPListItem newtask = taskList.Items.Add();
newtask["Title"] = "NewTask" + DateTime.Now.ToString();
newtask.Update();
}
}
}
SPTimerjobFeature1.EventReceiver.cs
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SPCustomTimerJob.SPJobs;
namespace SPCustomTimerJob.Features.SPTimerJobFeature
{
[Guid("65cf2a1a-6a7e-4426-b63d-9f0be40f85f7")]
public class SPTimerJobFeatureEventReceiver : SPFeatureReceiver
{
const string JobName = "SPTimer Job
Task";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication
parentwebApp = (SPWebApplication)properties.Feature.Parent;
SPSite site =
properties.Feature.Parent as SPSite;
DeleteExistingJob(JobName,
parentwebApp);
CreateJob(parentwebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication parentwebApp = (SPWebApplication)properties.Feature.Parent;
DeleteExistingJob(JobName, parentwebApp);
}
catch (Exception ex)
{
throw ex;
}
}
private bool CreateJob(SPWebApplication site)
{
bool jobCreated = false;
try
{
TimerJobClass job = new TimerJobClass(JobName, site);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
job.Schedule = schedule;
job.Update();
}
catch (Exception)
{
return jobCreated;
}
return jobCreated;
}
public bool DeleteExistingJob(string jobName, SPWebApplication site)
{
bool jobDeleted = false;
try
{
foreach (SPJobDefinition job in site.JobDefinitions)
{
if (job.Name == jobName)
{
job.Delete();
jobDeleted = true;
}
}
}
catch (Exception)
{
return jobDeleted;
}
return jobDeleted;
}
}
}
van
ReplyDeletekastamonu
elazığ
tokat
sakarya
1MEF
istanbul evden eve nakliyat
ReplyDeletebalıkesir evden eve nakliyat
şırnak evden eve nakliyat
kocaeli evden eve nakliyat
bayburt evden eve nakliyat
ZVTAU
5D5D8
ReplyDeleteÇankaya Boya Ustası
Bitmart Güvenilir mi
Hatay Şehir İçi Nakliyat
Zonguldak Parça Eşya Taşıma
Tekirdağ Fayans Ustası
Urfa Şehir İçi Nakliyat
Yozgat Şehirler Arası Nakliyat
Bitlis Parça Eşya Taşıma
Giresun Evden Eve Nakliyat
B955D
ReplyDeleteKonya Şehir İçi Nakliyat
Bitlis Lojistik
Kalıcı Makyaj
Gümüşhane Şehir İçi Nakliyat
Denizli Evden Eve Nakliyat
Bingöl Şehirler Arası Nakliyat
Zonguldak Lojistik
Balıkesir Şehir İçi Nakliyat
Iğdır Evden Eve Nakliyat
58542
ReplyDeleteHatay Parça Eşya Taşıma
Şırnak Evden Eve Nakliyat
Bartın Parça Eşya Taşıma
Maraş Şehir İçi Nakliyat
Iğdır Parça Eşya Taşıma
Yalova Evden Eve Nakliyat
Lbank Güvenilir mi
Tekirdağ Cam Balkon
Aksaray Evden Eve Nakliyat
0AD98
ReplyDeleteDiyarbakır Parça Eşya Taşıma
Giresun Şehirler Arası Nakliyat
Altındağ Boya Ustası
Cointiger Güvenilir mi
Ünye Çatı Ustası
Silivri Cam Balkon
Karapürçek Boya Ustası
Çerkezköy Halı Yıkama
Yobit Güvenilir mi
BDF3B
ReplyDeleteBilecik Lojistik
Kripto Para Borsaları
Urfa Şehir İçi Nakliyat
Etlik Boya Ustası
Çankaya Parke Ustası
Konya Evden Eve Nakliyat
Erzurum Evden Eve Nakliyat
Çerkezköy Halı Yıkama
Amasya Lojistik
5B850
ReplyDeletedüzce telefonda canlı sohbet
muş canlı sohbet odası
goruntulu sohbet
diyarbakır sesli sohbet siteleri
kırklareli görüntülü sohbet uygulama
erzincan ücretsiz sohbet odaları
mardin canli sohbet bedava
kütahya görüntülü sohbet kadınlarla
kadınlarla görüntülü sohbet
65C81
ReplyDeleteosmaniye bedava görüntülü sohbet sitesi
şırnak canlı ücretsiz sohbet
zonguldak canlı görüntülü sohbet odaları
Amasya Rastgele Sohbet Odaları
Çanakkale Kadınlarla Rastgele Sohbet
yalova sohbet odaları
bilecik rastgele görüntülü sohbet ücretsiz
bolu mobil sohbet
urfa canlı görüntülü sohbet odaları
524CA
ReplyDeleteparasız sohbet siteleri
istanbul bedava sohbet
Izmir Sesli Sohbet Siteleri
Amasya Sesli Sohbet Sesli Chat
Çanakkale Mobil Sohbet Chat
canlı görüntülü sohbet
görüntülü sohbet siteleri ücretsiz
Tunceli Canlı Sohbet Siteleri Ücretsiz
Diyarbakır Canlı Görüntülü Sohbet Odaları
A5D42
ReplyDeleteBitcoin Kazanma
Caw Coin Hangi Borsada
Baby Doge Coin Hangi Borsada
Twitter Takipçi Hilesi
Okex Borsası Güvenilir mi
Bitcoin Para Kazanma
Snapchat Takipçi Satın Al
Instagram Beğeni Satın Al
Alya Coin Hangi Borsada
67071
ReplyDeleteSonm Coin Hangi Borsada
Kripto Para Kazma
Periscope Beğeni Satın Al
Bitcoin Kazanma
Hamster Coin Hangi Borsada
Ön Satış Coin Nasıl Alınır
Soundcloud Dinlenme Satın Al
Soundcloud Dinlenme Hilesi
Facebook Grup Üyesi Hilesi