Timer Job is something which comes into mind when you are looking for batch process in sharepoint.In most of the realtime sharepoint site requires Pulling data from some other application and update the data in sharepoint.In some other scenario we need to synchronize information or Updata date in every day, or give the admin a flexibility execute some batch processing.Its realy good idea to create a Custom Scheduled Job.Let see a Basic skelton code.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
//Class should inherit from SPJobDefinition
Public class SampleJob : SPJobDefinition
{
//You could Define Custom properties
[Persisted()]
public string MyCustomProperty;
}
Here basically you will be inheriting from SPJobDefinition class and then implementing three of the constructors.Then comes main function Execute method , where all of your real excution code goes.You should Override this function to customize the job execution.Another good point you have to watch for the lock type you specifies in the constructor.
SPJobLockType:
ContentDatabase – this options locks the content database associated with the web application, but the job will be run for each content database associated with the web application.
Job – Ensures that the job will be run only on a single server.
None – Ensures that the Job will be run on all servers.
Next thing how we are going to deploy the Job.Create a feature and and implement FeatureActivated Event and FeatureDeActivating Event
public class SchedualedJobsFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPServer myServer = SPServer.Local;
SPWebApplication myApplication = site.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;
Type sPJobDefinitionType = typeof ( SPJobDefinition);
Type[] types = typeof (SchedualedJobsFeature).Assembly.GetTypes ();
foreach (Type t in types)
{
if (t.IsSubclassOf(sPJobDefinitionType))
{
object[] atts= t.GetCustomAttributes ( typeof ( MyJobDefinitionAttribute) , false );
if ( atts== null || atts.Length == 0)
{
continue;
}
MyJobDefinitionAttribute jobAttribute = (MyJobDefinitionAttribute) atts[0];
SPJobDefinition job = (SPJobDefinition)Activator.CreateInstance(t,
jobAttribute .Title, myApplication, myServer, jobAttribute .LockType );
job.Schedule = (SPSchedule) Activator.CreateInstance(jobAtt.ScheduleType);
try
{
SPJobDefinition oldJob = myJobsColl[jobAttribute .Title ];
if (oldJob != null)
{
myJobsColl.Remove(oldJob.Id);
}
else
{
}
}
catch {}
try
{
job.Status = SPObjectStatus.Disabled;
myJobsColl.Add(job);
}
catch (Exception ex)
{
//Handle Exception
}
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPServer myServer = SPServer.Local;
SPWebApplication myApplication = site.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;
Type sPJobDefinitionType = typeof(SPJobDefinition);
Type[] types = typeof(SchedualedJobsFeature).Assembly.GetTypes();
foreach (Type t in types)
{
if (t.IsSubclassOf(sPJobDefinitionType))
{
object[] atts = t.GetCustomAttributes(typeof(MyJobDefinitionAttribute), false);
if (atts == null || atts.Length == 0)
{
continue;
}
MyJobDefinitionAttribute jobAtt = (MyJobDefinitionAttribute)atts[0];
try
{
SPJobDefinition job = SalesCatalogListItemSync)myApplication.JobDefinitions[jobAtt.Title];
myApplication.JobDefinitions.Remove(job.Id);
}
catch (Exception ex)
{
//Handle Exeception
}
}
}
}
public class MyJobDefinitionAttribute: Attribute
{
public string Title
{
get;
set;
}
public Type ScheduleType
{
get;
set;
}
public SPJobLockType LockType
{
get;
set;
}
}
Looks simple rite.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
//Class should inherit from SPJobDefinition
Public class SampleJob : SPJobDefinition
{
//You could Define Custom properties
[Persisted()]
public string MyCustomProperty;
///Implement Following three constructors
public SampleJob (): base(){ }
public SampleJob (string jobName, SPService service, SPServer server, SPJobLockType targetType): base (jobName, service, server, targetType) { }
public SampleJob (string jobName, SPWebApplication webApplication): base (jobName, webApplication, null, SPJobLockType.Job) { }
//You should Override Execute Method
public override void Execute (Guid JobbId) {
//Your Code Execution
//In my case i will be calling a exeternal webservice and synchronize the Data
}
}
Here basically you will be inheriting from SPJobDefinition class and then implementing three of the constructors.Then comes main function Execute method , where all of your real excution code goes.You should Override this function to customize the job execution.Another good point you have to watch for the lock type you specifies in the constructor.
SPJobLockType:
ContentDatabase – this options locks the content database associated with the web application, but the job will be run for each content database associated with the web application.
Job – Ensures that the job will be run only on a single server.
None – Ensures that the Job will be run on all servers.
Next thing how we are going to deploy the Job.Create a feature and and implement FeatureActivated Event and FeatureDeActivating Event
public class SchedualedJobsFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPServer myServer = SPServer.Local;
SPWebApplication myApplication = site.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;
Type sPJobDefinitionType = typeof ( SPJobDefinition);
Type[] types = typeof (SchedualedJobsFeature).Assembly.GetTypes ();
foreach (Type t in types)
{
if (t.IsSubclassOf(sPJobDefinitionType))
{
object[] atts= t.GetCustomAttributes ( typeof ( MyJobDefinitionAttribute) , false );
if ( atts== null || atts.Length == 0)
{
continue;
}
MyJobDefinitionAttribute jobAttribute = (MyJobDefinitionAttribute) atts[0];
SPJobDefinition job = (SPJobDefinition)Activator.CreateInstance(t,
jobAttribute .Title, myApplication, myServer, jobAttribute .LockType );
job.Schedule = (SPSchedule) Activator.CreateInstance(jobAtt.ScheduleType);
try
{
SPJobDefinition oldJob = myJobsColl[jobAttribute .Title ];
if (oldJob != null)
{
myJobsColl.Remove(oldJob.Id);
}
else
{
}
}
catch {}
try
{
job.Status = SPObjectStatus.Disabled;
myJobsColl.Add(job);
}
catch (Exception ex)
{
//Handle Exception
}
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPServer myServer = SPServer.Local;
SPWebApplication myApplication = site.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;
Type sPJobDefinitionType = typeof(SPJobDefinition);
Type[] types = typeof(SchedualedJobsFeature).Assembly.GetTypes();
foreach (Type t in types)
{
if (t.IsSubclassOf(sPJobDefinitionType))
{
object[] atts = t.GetCustomAttributes(typeof(MyJobDefinitionAttribute), false);
if (atts == null || atts.Length == 0)
{
continue;
}
MyJobDefinitionAttribute jobAtt = (MyJobDefinitionAttribute)atts[0];
try
{
SPJobDefinition job = SalesCatalogListItemSync)myApplication.JobDefinitions[jobAtt.Title];
myApplication.JobDefinitions.Remove(job.Id);
}
catch (Exception ex)
{
//Handle Exeception
}
}
}
}
public class MyJobDefinitionAttribute: Attribute
{
public string Title
{
get;
set;
}
public Type ScheduleType
{
get;
set;
}
public SPJobLockType LockType
{
get;
set;
}
}
Looks simple rite.
thanks, alot. U saved my day
ReplyDeleteThe knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteOracle training in btm
Oracle Online Training
Oracle training in chennai
Oracle training in Bangalore
Excellent Blog! I would like to thank for the efforts you have made in writing this ExcelR Digital Marketing Classes In Pune post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
ReplyDeleteI just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteweb designing training in chennai
web designing training in tambaram
digital marketing training in chennai
digital marketing training in tambaram
rpa training in chennai
rpa training in tambaram
tally training in chennai
tally training in tambaram
Excellent Blog! I would like to thank for the efforts you have made in writing
ReplyDeletehardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr