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);
}
}