Thursday 9 June 2016

Create Timer Job Step By Step

1. Add new SharePoint Empty Project.
2. Add new class as below.

public class CustomTimerJob : SPJobDefinition
    {
        public CustomTimerJob() : base() { }
        public CustomTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {

        }
        public CustomTimerJob(string jobName, SPWebApplication webapp) :
            base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = jobName;
        }
        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApp = this.Parent as SPWebApplication;
            SPList taskList = webApp.Sites[0].RootWeb.Lists["ListName"];


            //SPListItem newTask = taskList.Items.Add();
            //newTask["Title"] = "NewTask";
            //newTask.Update();




                                                              OR



         
                string siteURL = this.Properties["siteurl"].ToString();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite spSite = new SPSite(siteURL))
                    {
                        using (SPWeb spWeb = spSite.OpenWeb())
                        {
                            SPList lst = spWeb.Lists["ListName"];
                            SPListItem itm = lst.Items.Add();
                            itm["Title"] = "NewTask";
                            itm.Update();
                        }
                    }
                });
           
        }
    }



Now Create New Feature Event Receiver and perform below steps in event receiver cs file

   private const string LIST_JOB = "Testing Timer Job";
   private const string LIST_JOB = "Testing Timer Job1";
   private const string LIST_JOB = "Testing Timer Job2";


        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            Guid SiteID = ((SPSite)properties.Feature.Parent).ID;
            SPSecurity.RunWithElevatedPrivileges(delegate(){
                SPSite site = new SPSite(SiteID);
                if (site != null)
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == LIST_JOB )
                            job.Delete();
                    }
                    CustomTimerJob TimeJob = new CustomTimerJob(LIST_JOB, site.WebApplication);
                    SPDailySchedule schedule = new SPDailySchedule();
                    schedule.BeginHour = 15;
                    schedule.BeginMinute = 30;
                    schedule.EndHour = 15;
                    schedule.EndMinute = 45;

                    TimeJob.Schedule = schedule;
                    TimeJob.Properties.Add("siteurl", site.Url);
                    TimeJob.Update();
                }
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == LIST_JOB )
                    job.Delete();
            }
        }


No comments:

Post a Comment