Mittwoch, 21. Juli 2010

Create custom timer-job - einen benutzerdefinierten Time-Job in Sharepoint 2010 erstellen

Um einen benutzerdefinierten TimerJob zu erstellen, einfach ein neues Visual Webpart Projekt im Visual Studio 2010 erstellen.
Dort braucht es dann nur eine Klassendatei, welche den Job definiert, so dass die Struktur wie folgt aussieht:

To create a custom timer-job, you just create a new Visual Webpart project in Visual Studio 2010.
There you just need one class-file, that defines the job, so the structure will look like this:
sharepoint project

Grossansicht anzeigen - Show large image


Was man noch benötigt ist den Feature.EventReceiver. Diesen erstellt man, in dem man auf die .feature Datei rechts klickt und
dort, wie in der Grafik unten, Add EventReceiver anklickt. Diese Datei enthält Code, welcher den Job initialisieren wird und
in diversen state-levels Code ausführen kann.

What you need then is an event-receiver. You can create on, in right clicking on the .feature file, like shown in the image,
and click on "Add EventReceiver". This file contains code, that will initialize the job and you can add code to different state-levels
to run code.
sharepoint project

Grossansicht anzeigen - Show large image


When das Projekt deployed wurde, kann man unter Site Settings -> Site collection features den Job aktivieren.

When you deployed the project, you can activate the job under site-settings -> site collection features.
sharepoint project

Grossansicht anzeigen - Show large image


Sobald dieser aktiviert ist, muss er unter Central Administration -> Monitoring -> Check job status -> Job Defenitions sichtbar sein.
(Seite hat meist mehrere Seiten)

As soon as the job is activated, it is supposed to be visible under central administration -> monitoring -> check job-status -> job-defenitions.
(Page usually has more pages)

In der linken Navigation befinden sich Punkte, welche man durchklicken kann und sieht, ob und wann der Job gelaufen ist, oder
laufen wird.

The left-hand side navigation has topics, that you can click through. There you can see if and when a job is run or will be run.
sharepoint project

Grossansicht anzeigen - Show large image


In die EventReceiver Datei kommt in die Methode FeatureActivated folgender Code zum initialisieren des Jobs:
Into the EventReceiver file, into the FeatureActivated method, you can add the following code to initialize the job:

SPSite spSite = (SPSite)properties.Feature.Parent;

SPWebApplication webApp = spSite.WebApplication;
//SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

if (webApp == null)
throw new SPException("Error obtaining reference to Web application.");

// Ensure the job is not already registered.
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == JOB_NAME)
{
job.Delete();
}
}

// Install job.
MyCustomTimerJob customTimerJob = new MyCustomTimerJob(webApp);
// Schedule the job to run every 5 minute all the time.
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
customTimerJob.Schedule = schedule;
// Save changes.
customTimerJob.Update();


In die FeatureDeactivating Methode kommt dieser Code:
Into the FeatureDeactivating method you can add this code:

SPSite spSite = (SPSite)properties.Feature.Parent;

SPWebApplication webApp = spSite.WebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == JOB_NAME) job.Delete();


In die MyCustomTimerJob.cs Datei wird folgender Code eingefügt:
Insert the following code into the MyCustomTimerJob.cs file:

public class MyCustomTimerJob : SPJobDefinition
{
private static const string JOB_NAME = "MyCustomTimerJob";
public MyCustomTimerJob() : base() { }
public MyCustomTimerJob(SPWebApplication webApp)
: base(JOB_NAME, webApp, null, SPJobLockType.Job)
{
Title = JOB_NAME;
}

public override void Execute(Guid targetInstanceId)
{
//Code here, what the timer job is supposed to do
}
}


TIP:
Versuchen Sie in der Execute Methode eine Assembly oder ein Objekt aufzurufen, Hier nicht unbedingt den ausführenden Code einfügen.
Try to call just some assemblies and objects from the Execute method. Don't put the execution code directly in there.

Keine Kommentare:

Kommentar veröffentlichen