您的位置:首页 > 其它

Creating Custom SharePoint Timer Jobs

2008-03-01 16:03 363 查看
摘自:[http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx]

ThefirstthingyouneedtodoiscreateaclassthatinheritsfromtheMicrosoft.SharePoint.Administration.SPJobDefinitionclass.Toimplementthisclass,youneedtocreateafewconstructorsandoverridetheExecute()method,likeso:

namespaceAndrewConnell.TaskLogger{

[code]publicclassTaskLoggerJob:SPJobDefinition{

publicTaskLoggerJob()

:base(){
}


publicTaskLoggerJob(stringjobName,SPServiceservice,SPServerserver,SPJobLockTypetargetType)

:base(jobName,service,server,targetType){
}


publicTaskLoggerJob(stringjobName,SPWebApplicationwebApplication)

:base(jobName,webApplication,null,SPJobLockType.ContentDatabase){

this.Title="TaskLogger";
}


publicoverridevoidExecute(GuidcontentDbId){

//getareferencetothecurrentsitecollection'scontentdatabase

SPWebApplicationwebApplication=this.ParentasSPWebApplication;

SPContentDatabasecontentDb=webApplication.ContentDatabases[contentDbId];


//getareferencetothe"Tasks"listintheRootWebofthefirstsitecollectioninthecontentdatabase

SPListtaskList=contentDb.Sites[0].RootWeb.Lists["Tasks"];


//createanewtask,settheTitletothecurrentday/time,andupdatetheitem

SPListItemnewTask=taskList.Items.Add();

newTask["Title"]=DateTime.Now.ToString();

newTask.Update();
}
}
}

[/code]
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}]]>
Asyoucansee,thisjobdoesnothingimportantbutaddanewitemtoaTasklisteverytimeit'sexecuted.Nowthatyouhavethejobbuilt,youneedtogetitregistered.Unfortunatelytheonlywaytodothisisthroughcode,butitsurewouldbeoneheckofacandidateforacustomSTSADMcommand.Anyway,sincewedon'thavethattoday,Iliketousethefeatureactivated&deactivatedeventstoinstall/uninstallmycustomjobs.

Todothis,youhavetocreateanotherclassthatinheritsfromtheMicrosoft.SharePoint.SPFeatureReceiverclassandimplementtheFeatureActivated&FeatureDeactivatedeventhandlerslikeso:

namespaceAndrewConnell.TaskLogger{



classTaskLoggerJobInstaller:SPFeatureReceiver{



conststringTASK_LOGGER_JOB_NAME="TaskLogger";



publicoverridevoidFeatureInstalled(SPFeatureReceiverPropertiesproperties){


}






publicoverridevoidFeatureUninstalling(SPFeatureReceiverPropertiesproperties){


}






publicoverridevoidFeatureActivated(SPFeatureReceiverPropertiesproperties){



SPSitesite=properties.Feature.ParentasSPSite;






//makesurethejobisn'talreadyregistered



foreach(SPJobDefinitionjobinsite.WebApplication.JobDefinitions){



if(job.Name==TASK_LOGGER_JOB_NAME)



job.Delete();


}






//installthejob



TaskLoggerJobtaskLoggerJob=newTaskLoggerJob(TASK_LOGGER_JOB_NAME,site.WebApplication);






SPMinuteScheduleschedule=newSPMinuteSchedule();



schedule.BeginSecond=0;



schedule.EndSecond=59;



schedule.Interval=5;



taskLoggerJob.Schedule=schedule;






taskLoggerJob.Update();


}






publicoverridevoidFeatureDeactivating(SPFeatureReceiverPropertiesproperties){



SPSitesite=properties.Feature.ParentasSPSite;






//deletethejob



foreach(SPJobDefinitionjobinsite.WebApplication.JobDefinitions){



if(job.Name==TASK_LOGGER_JOB_NAME)



job.Delete();


}


}


}


}


Now...togetitworking,allyouneedtodois:

DeploythestronglynamedassemblytotheGAC.
ResetIIS(requiredforSharePointto"see"thenewtimerjobintheGAC).
Createafeaturespecifyingthereceiverclassandassemblythatcontainstheeventreceivers.
Installthefeature.
Activatethefeature.

Thissampletimerjobassumesit'srunningwithinthecontextofaWSSv3sitethathasalistcreatedwiththeTaskslisttemplateintherootwebwithinthesitecollection(or,justcreateaTeamSiteattherootofyoursitecollection).

Onceyouactivatethefeature,itshouldshowupontheTimerJobDefinitionspageinCentralAdministration/Operations.Itwon'tappearintheTimerJobStatuspageuntilit'sexecutedatleastonetime.Waitforfiveminutesorso(theschedulethissampleisusing)toseeifit'srunning.YoushouldstarttoseeitemsshowingupintheTaskslistintherootsiteinyoursitecollection.

Here'saVisualStudio2005solutionthatincludeseverythingyouneedtocreateacustomtimerjob.NotethatIusedthetechniqueIdescribedinthisarticletomodifytheVisualStudioprojecttocreatetheWSPfileforme:

»TaskLogger_CustomTimerJob.zip

Or,youcanusethisWindowsSharePointSolutionPackage(*.WSP)todeploytheprebuilttimerjobusedinthisarticle(handlessteps1-4above):

»AndrewConnell.TaskLoggerJob.zip(unpacktheZIPtogettheWSP)

TodeploytheWSPfile,simplyaddittothesolutionstoreusingSTSADM(usingthefollowingcommand)andthendeployittothedesiredsite:

stsadm–oaddsolution–filenameAndrewConnell.TaskLoggerJob.wsp

Note:ifyouplantotestthetimerjobattachedinthatarticle,pleasemakesureyoualsouninstallit.IjustrealizedIneveruninstalledmineandmyTaskslisthadover30,000itemsinitfromthelastfewweeks.Oops!:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: