SharePoint Development Timerjob Introduction

Source: Internet
Author: User
Tags foreach tostring

The project needs to write Timerjob, before also probably know the principle, however, the development process, or encountered some problems, online read a lot of blogs, but also write the ash is often good, but, they still want to write again, also be given a summary of their own, also calculate for everyone more a reference bar.

TIMERJOB project structure, there are two classes, one is used to define the Timerjob function, one is to deploy the development of a good timerjob, respectively, inherit two different class. As shown in the following diagram, you first build a project with the following structure:

File Description:

Timerjob definition class: ModifyTitle.cs (inherited from Spjobdefinition)

Timerjob Installation class: ModifyTitleInstall.cs (inherited from SPFeatureReceiver)

Activate the Timerjob feature.xml

Add enhanced naming because future DLLs will be built into the GAC

To add a reference:

Reference Microsoft.SharePoint.dll file, two class all need to add the following namespaces

Using Microsoft.SharePoint;

Using Microsoft.SharePoint.Administration;

Modifytitleinstall class

public class Modifytitleinstall:spfeaturereceiver
{
Const string timerjobname = "Modifytitletimerjob"; title of//timerjob
Ways to activate Timerjob
public override void FeatureActivated (Spfeaturereceiverproperties properties)
{
SPSite site = properties. Feature.parent as SPSite;
foreach (spjobdefinition job in site.) Webapplication.jobdefinitions)
{
If you have the same timerjob, delete the first
if (job. Title = = timerjobname)
{
Job. Delete ();
}
}
Modifytitle modifytitle = new Modifytitle (Timerjobname, site. WebApplication);
Spminuteschedule minuteschedule = new Spminuteschedule ();//Timer Object
Minuteschedule.beginsecond = 0;
Minuteschedule.endsecond = 59;
Minuteschedule.interval = 1;
Modifytitle.schedule = Minuteschedule;
Modifytitle.update ();
throw new NotImplementedException ();
}
public override void FeatureDeactivating (Spfeaturereceiverproperties properties)
{
SPSite site = properties. Feature.parent as SPSite;
foreach (spjobdefinition job in site.) Webapplication.jobdefinitions)
{
if (job. Title = = timerjobname)
{
Job. Delete ();
}
}
throw new NotImplementedException ();
}
public override void Featureinstalled (Spfeaturereceiverproperties properties)
{
throw new NotImplementedException ();
}
public override void Featureuninstalling (Spfeaturereceiverproperties properties)
{
throw new NotImplementedException ();
}

Modifytitle class

public class ModifyTitle : SPJobDefinition
{
public ModifyTitle():base(){}
public ModifyTitle(string TimerName, SPWebApplication webapp) : base(TimerName, webapp, null, SPJobLockType.ContentDatabase)
{
//TimerJob的标题
this.Title = "定期修改Title的TimerJob";
}
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webapp = this.Parent as SPWebApplication;
SPContentDatabase contentDB=webapp.ContentDatabases[targetInstanceId];
foreach (SPItem item in contentDB.Sites[0].RootWeb.Lists["TimerJob"].Items)
{
DateTime dt = Convert.ToDateTime(item["创建时间"].ToString());
item["标题"] = "今天是这个月的第" + dt.Day.ToString() + "天";
item.Update();
}
//base.Execute(targetInstanceId);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.