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:
[code]publicclassTaskLoggerJob:SPJobDefinition{
[/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:
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!:)
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,butitsurewouldbeoneheckofacandidatefora
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.NotethatIused
»
Or,youcanusethisWindowsSharePointSolutionPackage(*.WSP)todeploytheprebuilttimerjobusedinthisarticle(handlessteps1-4above):
»
TodeploytheWSPfile,simplyaddittothesolutionstoreusingSTSADM(usingthefollowingcommand)andthendeployittothedesiredsite:
stsadm–oaddsolution–filenameAndrewConnell.TaskLoggerJob.wsp
Note:ifyouplantotestthetimerjobattachedinthatarticle,pleasemakesureyoualsouninstallit.IjustrealizedIneveruninstalledmineandmyTaskslisthadover30,000itemsinitfromthelastfewweeks.Oops!:)
相关文章推荐
- Creating Custom Timer Jobs in Sharepoint
- Debugging Custom SharePoint Timer Jobs
- Creating a Custom Login Page for SharePoint 2010
- Custom redirect after creating a new Sharepoint Item
- Creating a Custom SharePoint 2007 Portal Site Definition using the PortalProvisioningProvider Class
- Creating a Custom SharePoint 2007 List Definition
- Creating a Custom Page Layout in SharePoint 2013
- Creating a Custom Login Page for SharePoint 2010
- Creating a Custom SharePoint 2007 List Definition
- SP2010开发和VS2010专家"食谱"--第六章节--Web Services和REST(6)--Creating a custom SharePoint WCF service
- SharePoint: Creating a custom field
- SP2010开发和VS2010专家"食谱"--第六章节--Web Services和REST(6)--Creating a custom SharePoint WCF service
- Sharepoint Custom Control Creating custom Multi Choice field type
- 学习:Creating a Custom Web Service for SharePoint(转)
- sharepoint ECMA using a custom contentType to creating a list in SubSite
- Sharepoint Custom Timer Job (Sharepoint 自定义定时器,定时工作,定时启动)
- Sharepoint Custom Timer Job
- Creating Custom Connector Sending Claims with SharePoint 2013
- SPJS Upload for SharePoint: Custom upload page for uploading documents to various document libraries in a site collection
- Creating SharePoint 2010 Event Receivers in Visual Studio 2010