zoukankan      html  css  js  c++  java
  • SharePoint 2010 Custom Timer Job

    Introduction

    Based on my popular articel about Creating Custom Timer Job in Sharepoint 2010,I Have decide to upload the simple code  on how to create Custom Timer Job in Sharepoint 2010 ,But first let us know more about Timer Job

    • A timer job runs in a specific Windows service for SharePoint Server.
    • Timer jobs also perform infrastructure tasks for the Timer service, such as clearing the timer job history and recycling the Timer service; and tasks for Web applications, such as sending e-mail alerts.
    • A timer job contains a definition of the service to run and specifies how frequently the service is started.
    • The SharePoint 2010 Timer service (SPTimerv4) runs timer jobs.
    • Many features in SharePoint Server rely on timer jobs to run services according to a schedule.

    Building the Sample

    To run the sample you just need to open it in Visual Studio 2010 then deploy it to your test machine or you can package it to deploy it in Production environment.The solution itself will create list for you and try to fill it each five minutes.

    Description

    Based on my knoladge Customer Timer job in sharepoint 2010 is on of the most confused approch in SharePoint Many developer stuck at some point and they do not what to do to make it run correctly.So this sample will show you how to make it happent and run Custom Timer Job to achive you tasks in simple way.

    C#
    //Create class derived from SPJonDefinition Class 
     class ListTimerJob : SPJobDefinition 
        { 
             public ListTimerJob() 
     
                : base() 
            { 
     
            } 
     
            public ListTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) 
     
                : base(jobName, service, server, targetType) 
            { 
     
            } 
     
            public ListTimerJob(string jobName, SPWebApplication webApplication) 
     
                : base(jobName, webApplication, null, SPJobLockType.ContentDatabase) 
            { 
     
                this.Title = "List Timer Job"; 
     
            } 
     
            public override void Execute(Guid contentDbId) 
            { 
     
                // get a reference to the current site collection's content database 
     
                SPWebApplication webApplication = this.Parent as SPWebApplication; 
     
                SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; 
     
                // get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database 
     
                SPList Listjob = contentDb.Sites[0].RootWeb.Lists["ListTimerJob"]; 
     
                // create a new list Item, set the Title to the current day/time, and update the item 
     
                SPListItem newList = Listjob.Items.Add(); 
     
                newList["Title"] = DateTime.Now.ToString(); 
     
                newList.Update(); 
     
            } 
    } 
    //Add Event receiver at Feature Level  
    [Guid("9a724fdb-e423-4232-9626-0cffc53fb74b")] 
    public class Feature1EventReceiver : SPFeatureReceiver 
        { 
            const string List_JOB_NAME = "ListLogger"; 
            // Uncomment the method below to handle the event raised after a feature has been activated. 
     
            public override void FeatureActivated(SPFeatureReceiverProperties properties) 
            { 
                SPSite site = properties.Feature.Parent as SPSite; 
     
                // make sure the job isn't already registered 
     
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
                { 
     
                    if (job.Name == List_JOB_NAME) 
     
                        job.Delete(); 
     
                } 
     
                // install the job 
     
                ListTimerJob listLoggerJob = new ListTimerJob(List_JOB_NAME, site.WebApplication); 
     
                SPMinuteSchedule schedule = new SPMinuteSchedule(); 
     
                schedule.BeginSecond = 0; 
     
                schedule.EndSecond = 59; 
     
                schedule.Interval = 5; 
     
                listLoggerJob.Schedule = schedule; 
     
                listLoggerJob.Update(); 
     
            } 
     
            // Uncomment the method below to handle the event raised before a feature is deactivated. 
     
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
            { 
                SPSite site = properties.Feature.Parent as SPSite; 
     
                // delete the job 
     
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
                { 
     
                    if (job.Name == List_JOB_NAME) 
     
                        job.Delete(); 
     
                } 
     
    }  
    //That's all

    Source Code Files

    • SharePoint Project (CustomTimerJob) - contain List I
    • Source  ------http://code.msdn.microsoft.com/office/SharePoint-2010-Custom-416cd3a1
  • 相关阅读:
    jQuery 对象 等操作
    根据文件大小自动判断单位B,KB,MB,GB
    PHP 根据子ID递归获取父级ID,实现逐级分类导航效果
    JQuery 目录树jsTree插件用法
    关于循环列表中包含递归函数的问题
    PHP文件上传大小限制问题
    UEditor+七牛,实现图片直连上传
    修改Ueditor的图片上传地址
    Thinkphp3.2.3加载外部类并调用类里面的方法 获取token
    七牛云--开发笔记
  • 原文地址:https://www.cnblogs.com/ilawrence/p/2828028.html
Copyright © 2011-2022 走看看