Friday, June 7, 2013

Custom Timer Job Sharepoint

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace GENERAL_CustomTimerJob
{
    class GENERAL_CustomTimerJob : SPJobDefinition
    {
        #region Constructors
        public GENERAL_CustomTimerJob()
            : base()
        {
        }
        public GENERAL_CustomTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }
        public GENERAL_CustomTimerJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Simple Example Timer Job";
        }
        #endregion

        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApp = this.Parent as SPWebApplication;
            SPSiteCollection timerSiteCollection = webApp.ContentDatabases[targetInstanceId].Sites;
            SPList timerJobList = null;
            foreach (SPSite site in timerSiteCollection)
            {
                timerJobList = site.RootWeb.Lists.TryGetList("Announcements");
                if (timerJobList != null)
                {
                    SPListItem newItem = timerJobList.Items.Add();
                    newItem["Title"] = "Today is " + DateTime.Today.ToLongDateString();
                    newItem.Update();
                }
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;

namespace GENERAL_CustomTimerJob.Features.CustomTimerJobFeature
{
    [Guid("1b45bf8b-f5e2-4155-8f7f-999b1163c3d1")]
    public class CustomTimerJobFeatureEventReceiver : SPFeatureReceiver
    {
        const string TIMER_JOB_NAME = "DemoTimerJob";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = (SPSite)properties.Feature.Parent;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == TIMER_JOB_NAME)
                {
                    job.Delete();
                }
            }
            GENERAL_CustomTimerJob newTimerJob = new GENERAL_CustomTimerJob(TIMER_JOB_NAME, site.WebApplication);
            SPMinuteSchedule jobSchedule = new SPMinuteSchedule();
            jobSchedule.BeginSecond = 0;
            jobSchedule.EndSecond = 59;
            jobSchedule.Interval = 5;
            newTimerJob.Schedule = jobSchedule;
            newTimerJob.Update();
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == TIMER_JOB_NAME)
                {
                    job.Delete();
                }
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Featured Post

Sent Email in C#

Sent Email in C# : //Option 1: using S ystem; using System.Net.Mail; using System.Security; using System.Text; using System.Threading.T...

Popular posts