Create a Custom Timer Job in SharePoint 2010
Step by Step Procedure to create a custom Timer Job.
Open Microsoft Visual Studio as "Run as Administrator"
Create a New Project
Create a Empty SharePoint Project
Connect to the Portal and "Deploy as Farm Solution"
Add a New Item in the Project
Add a Class File to the Project
Change the Class as SPJobDefinition
Here is the code to be placed inside the Class File
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;
namespace SharePointSuresh{
class SharePointSuresh : SPJobDefinition{ public SharePointSuresh(): base(){}
public SharePointSuresh(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType){}
public SharePointSuresh(string jobName, SPWebApplication webApplication, SPServer server, SPJobLockType lockType): base(jobName, webApplication, server, lockType){
this.Title = jobName;}
public SharePointSuresh(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.Job){
this.Title = jobName;}
public SharePointSuresh(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase){
this.Title = jobName;}
public SharePointSuresh(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.None){
this.Title = jobName;}
1.SPJobLockType.None -- if you set it none, the instance will run in all the available servers in the Farm (e.g. Application Server Timer Job)
2.SPJobLockType.ContentDatabase – this will cause instances to be running in each of the Web-Frontends.
3.SPJobLockType.Job – this will cause only one instance of the job to run on any of the front-end servers. (Note: it is possible to see multiple instances listed in the Job Status .. but if you look at the time it was last run.. only one would have run lately)
public override void Execute(Guid contentDbId)
{
// get a reference to the current site collection's content databaseSPWebApplication webApplication = this.Parent as SPWebApplication;
using (SPSite objSite = webApplication.Sites["SITEURL"]){
SPWeb objWeb = objSite.OpenWeb();
objWeb.AllowUnsafeUpdates = true;
// Write your code here
objWeb.AllowUnsafeUpdates = false;};
}
}
}
After the coding inside the class file. Insert a Feature
Insert a Event Receiver
Find below the coding in EventReceiver File
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace SharePointSuresh.Features.SharePointSureshFeature{
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public class SharePointSureshFeatureEventReceiver : SPFeatureReceiver{
public string JobName = "";
public string servername = "";
public override void FeatureActivated(SPFeatureReceiverProperties properties){
SPSite site = properties.Feature.Parent as SPSite;
JobName = properties.Feature.Properties["JobTitle"].Value;
servername = properties.Feature.Properties["servername"].Value;
DeleteJob(site); // Delete Job if already Exists
CreateJob(site); // Create new Job }
public override void FeatureDeactivating(SPFeatureReceiverProperties properties){
DeleteJob(properties.Feature.Parent as SPSite);
}
private void CreateJob(SPSite site){
SPServer server = site.WebApplication.Farm.Servers[servername];
SharePointSuresh job = new SharePointSuresh(JobName, site.WebApplication, server, SPJobLockType.Job);
SPMonthlySchedule schedule = new SPMonthlySchedule();
schedule.BeginDay = 1;
schedule.BeginHour = 5;
schedule.EndHour = 6;
schedule.BeginMinute = 10;
schedule.EndMinute = 59;
job.Schedule = schedule;
job.Update();
SPDailySchedule schedule = new SPDailySchedule();
schedule.BeginHour = 0;
schedule.BeginMinute = 0;
schedule.BeginSecond = 0;
schedule.EndHour = 3;
schedule.EndMinute = 59;
schedule.EndSecond = 59;
job.Schedule = schedule;
job.Update();
SPHourlySchedule scheduleHour = new SPHourlySchedule();
scheduleHour.BeginMinute = 4;
scheduleHour.EndMinute = 5;
job.Schedule = scheduleHour;
job.Update();
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 5;
schedule.Interval = 5;
job.Schedule = schedule;
job.Update();
}
private void DeleteJob(SPSite site){
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
if (job.Name == JobName)
job.Delete();
}
}
}
Open Feature.xml file, paste the below code and make necessary changes
<?xml version="1.0" encoding="utf-8" ?><
Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ReceiverAssembly="SharePointSuresh, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" ReceiverClass="$SharePoint.Type.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.FullName$" Scope="Site" Title="SharePointSureshFeature"><
Properties xmlns="http://schemas.microsoft.com/sharepoint/"><!--
The name of the timer job (will appear in Central Admin). --><
Property Key="JobTitle" Value="SharePointSuresh" /><!--
Server Name that the TimerJob will use --><
Property Key="servername" Value="SERVERNAME" /></
Properties></
Feature>
It can be almost impossible to find well-qualified users on this matter, however, you look like you be aware of exactly what you’re covering!
ReplyDeletesharepoint 2013 administration certification