Monday, March 15, 2010

TimerJob, Feature Receiver, Feature and Batch Files to Install Timer Job

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace PubFormLibraryTimer {
public class FormLibraryTimerJob : SPJobDefinition
{
private const string Event_Source = "FormLibraryTimerJob";

 public FormLibraryTimerJob () : base() { }
  public FormLibraryTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) : base (jobName, service, server, targetType) { }
public FormLibraryTimerJob(string jobName, SPWebApplication webApplication) : base (jobName, webApplication, null, SPJobLockType.ContentDatabase)
{ this.Title = jobName; }
public override void Execute(Guid targetInstanceId)
{
            DateTime fromTime = DateTime.Now.AddMinutes(-5);
DateTime toTime = DateTime.Now;
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
SPList formsLibraryList = contentDb.Sites[0].RootWeb.Lists["YourFormLibrary"];
            SPQuery oQuery = new SPQuery();
            oQuery.Query = "" + toTime + "" + "" + fromTime + "";
SPListItemCollection collListItems = formsLibraryList.GetItems(oQuery);
this.LogEventMessage(collListItems.Count);
}

  private void LogEventMessage(int count)
{
if (!EventLog.SourceExists(Event_Source))
{ EventLog.CreateEventSource(Event_Source, "Application"); }
EventLog.WriteEntry(Event_Source, "Your Form Library Modified Forms Count In Last 5 mins - " + count.ToString(), EventLogEntryType.Information);
}
}
}






Feature Receiver:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace YourFormLibraryTimer
{

public class FormsLibraryTimerJobFeatureReceiver : SPFeatureReceiver
{
private const string LOGGER_JOB_NAME = "FormLibraryTimerJob"; public override void FeatureInstalled (SPFeatureReceiverProperties properties) { } public override void FeatureUninstalling (SPFeatureReceiverProperties properties) { } public override void FeatureActivated (SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == LOGGER_JOB_NAME) job.Delete(); } FormLibraryTimerJob loggerJob = new FormLibraryTimerJob(LOGGER_JOB_NAME, site.WebApplication); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 59; schedule.Interval = 5; loggerJob.Schedule = schedule; loggerJob.Update(); } public override void FeatureDeactivating (SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == LOGGER_JOB_NAME) job.Delete(); } } } } Feature: ?xml version="1.0" encoding="utf-8" ? Feature Id="{DD1D417C-746B-4793-BA4E-A95F156E1E30}" Title="Form Library Timer Job" Description="Installs Form Library Timer Job" Version="1.0.0.0" Scope="Site" ReceiverAssembly="YourFormLibraryTimer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=68d32dec24dfb1a9"ReceiverClass="YourFormLibraryTimer.FormsLibraryTimerJobFeatureReceiver" xmlns="http://schemas.microsoft.com/sharepoint/" / Installing Timer Job in Batch Files: @SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions\12\Template" @SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm" @SET GACUTIL="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe" Echo Installing PubFormLibraryTimer.dll in GAC %GACUTIL% -if bin\debug\YourFormLibraryTimer.dll Echo Copying files to TEMPLATE directory xcopy /e /y TEMPLATE\* %TEMPLATEDIR% %STSADM% -o installfeature -filename FormLibraryTimerJob\feature.xml -force %STSADM% -o activatefeature -filename FormLibraryTimerJob\feature.xml -force -url http://localhost:91IISRESET

No comments: