woensdag 19 november 2008

Sharepoint: Creating TimerJobs

Feature.XML

<Feature

Id ="{ECEC9250-C195-4619-8580-45E4E0D0A84B}"

Title ="Title"

Description ="Timer Job for updating the taks"

Scope="Site"
Hidden="True"
xmlns=http://schemas.microsoft.com/sharepoint/
ReceiverAssembly="TimerJob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
ReceiverClass="NameSpace.TaskLoggerJobInstaller">
<
ElementManifests>
</ElementManifests>
</
Feature>

TaskloggerJob.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration ;
using Microsoft.SharePoint;
using System.Configuration;
using System.Web.Configuration;

namespace Namespace
{
public class TaskLoggerJob : SPJobDefinition{
/// <summary>
/// Initializes a new instance of the <see cref="TaskLoggerJob"/> class.
/// </summary>
public TaskLoggerJob ()
: base(){
}

/// <summary>
/// Initializes a new instance of the <see cref="TaskLoggerJob"/> class.

/// </summary>
/// <param name="jobName">Name of the job.</param>
/// <param name="service">The service.</param>
/// <param name="server">The server.</param>
/// <param name="targetType">Type of the target.</param>
public TaskLoggerJob (string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base (jobName, service, server, targetType) {
}

/// <summary>
/// Initializes a new instance of the <see cref="TaskLoggerJob"/> class.
/// </summary>
/// <param name="jobName">Name of the job.</param>
/// <param name="webApplication">The web application.</param>
public TaskLoggerJob (string jobName, SPWebApplication webApplication)
: base (jobName, webApplication, null, SPJobLockType.ContentDatabase) {
this.Title = "TaskloggerJob";
}

/// <summary>
/// Executes the specified content db id.
/// </summary>
/// <param name="contentDbId">The content db id.</param>
public override void Execute (Guid contentDbId) {
//// get a reference to the current site collection's content database

//// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database

//// create a new task, set the Title to the current day/time, and update the item

string siteCollectionUrl = string.Empty;
try
{
// Read WebAppName out of the Config File
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", ConfigurationManager.AppSettings["TimerJobs.WebAppName"]);
siteCollectionUrl = config.AppSettings.Settings["SiteName"].Value;

if (siteCollectionUrl.Length < 10)
{
throw new Exception("SiteCollection Name could not be found in the Configuration file or is invalid.");
}
}
catch (Exception exc)
{
throw new Exception("SiteCollection Name could not be found in the Configuration file.", exc.InnerException);
}
try
{
ToDoNightlyUpdate nightlyUpdate = new ToDoNightlyUpdate();
nightlyUpdate.NightlyUpdate(siteCollectionUrl);
}
catch (Exception exc)
{
throw exc;
}

}
}
}

TaskLoggerJobInstaller.cs

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


namespace
namespace {

class TaskLoggerJobInstaller : SPFeatureReceiver {
const string TASK_LOGGER_JOB_NAME = "Task_Logger_Job_Name";
/// <summary>
/// Features the installed.
/// </summary>
/// <param name="properties">The properties.</param>
public override void FeatureInstalled (SPFeatureReceiverProperties properties) {
}

/// <summary>
/// Features the uninstalling.
/// </summary>
/// <param name="properties">The properties.</param>
public override void FeatureUninstalling (SPFeatureReceiverProperties properties) {
}

/// <summary>
/// Features the activated.
/// </summary>
/// <param name="properties">The properties.</param>
public override void FeatureActivated (SPFeatureReceiverProperties properties) {
SPSite site = properties.Feature.Parent as SPSite;

// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TASK_LOGGER_JOB_NAME)
job.Delete();
}

// install the job
TaskLoggerJob taskLoggerJob = new TaskLoggerJob(TASK_LOGGER_JOB_NAME, site.WebApplication);

SPDailySchedule schedule = new SPDailySchedule();
schedule.BeginHour = 0;
schedule.BeginMinute = 1;
schedule.BeginSecond = 1;

schedule.EndHour = 1;
schedule.EndMinute = 1;
schedule.EndSecond = 59;


taskLoggerJob.Schedule = schedule;

taskLoggerJob.Update();
}

/// <summary>
/// Features the deactivating.
/// </summary>
/// <param name="properties">The properties.</param>
public override void FeatureDeactivating (SPFeatureReceiverProperties properties) {
SPSite site = properties.Feature.Parent as SPSite;

// delete the job
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) {
if (job.Name == TASK_LOGGER_JOB_NAME)
job.Delete();
}
}
}
}

1 opmerking:

SharePoint Experts India zei

Good article!thanks a lot for the information!