home screen

Search



Number Of Result : 0

Result :


Saturday, April 3, 2010

Create a Timer Job in SharePoint

- Create Feature to create timer job(FeatureActived) and delete Timer job(FeatureDeactiving)


- tại Attribute ReceiverAssembly: tùy vào DLL mà bạn compile(dùng Reflector để biết Assembly nó là gì)

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="b50e6c69-e1dc-4855-b2af-76439b86b049"
Title="Development - My Timer Job"
Description="Description for FeatureTimerJob"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
ReceiverAssembly="TimerJobDevelopment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=009f6dfe520f2069"
ReceiverClass="TimerJobDevelopment.MyFeatureReceiver"
AlwaysForceInstall="TRUE"
ActivateOnDefault="FALSE"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TimerJobDevelopment
{
public class MyFeatureReceiver : SPFeatureReceiver
{
string timerJobName = "HighPriorityBugAlert";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication app;
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
app = siteCollection.WebApplication;
else
{
SPWeb site = properties.Feature.Parent as SPWeb;
app = site.Site.WebApplication;
}
foreach (SPJobDefinition job in app.JobDefinitions)
{
if (job.Name == timerJobName)
job.Delete();
}
HighPriorityBugAlert timerJob = new HighPriorityBugAlert(timerJobName, app, null, SPJobLockType.ContentDatabase);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 2;
timerJob.Schedule = schedule;

timerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication app;
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
app = siteCollection.WebApplication;
else
{
SPWeb site = properties.Feature.Parent as SPWeb;
app = site.Site.WebApplication;
}
foreach (SPJobDefinition job in app.JobDefinitions)
{
if (job.Name == timerJobName)
job.Delete();
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling( SPFeatureReceiverProperties properties)
{
}
}
}


- Create Custom Timer Job

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace TimerJobDevelopment
{
public class HighPriorityBugAlert : SPJobDefinition
{
public HighPriorityBugAlert() : base() { }
public HighPriorityBugAlert(string name, SPWebApplication webApplication,
SPServer server, SPJobLockType lockType)
: base(name, webApplication, server, lockType) { }
public HighPriorityBugAlert(string name, SPService service,
SPServer server, SPJobLockType lockType)
: base(name, service, server, lockType) { }
public override void Execute(Guid targetInstanceId)
{

SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];

SPList bugs = contentDb.Sites[0].AllWebs["Docs"].Lists["Bugs"];
string body = " <b> New high priority bugs: </b> <br/> ";
foreach (SPListItem item in bugs.Items)
{
body += item.Title + " <br/> ";
}

SPList announcements = contentDb.Sites[0].AllWebs["Docs"].Lists["Announcements"];
SPListItem announcement = announcements.Items.Add();
announcement["Title"] = "High Priority Bugs";
announcement["Body"] = body;
announcement.Update();
}

}
}

1 comment:

SharePointTaskMaster said...

Simle way to make a Timer Job Feature for SharePoint 2010 / Cách tạo một tính năng chạy theo lịch trình cho SharePoint 2010 http://sharepointtaskmaster.blogspot.com/2011/05/simle-way-to-make-timer-job-feature-for.html