Wednesday, 16 September 2015

SharePoint 2010 Create Monthly Timer Job Which Execut On Last Day Of Month

      
 MonthlyTJService is helper class in which your logic for timer job is implemented



       string MonthlyTJ = "Monthly TJ";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
                 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite site = properties.Feature.Parent as SPSite;
                    DeleteJob(site);
                    CreateJob(site);
                });
          
         }

        private static void DeleteJob(SPSite site)
        {
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                 job.Delete();
            }
        }

        private static void CreateJob(SPSite site)
        {
            if (site == null)
                throw new ArgumentNullException("site", "site is null.");
          
            MonthlyTJService monthlyTJ = new MonthlyTJService(MonthlyTJ, site.WebApplication);
            SPMonthlySchedule monthlyTJSchedule = new SPMonthlySchedule()
            {
                BeginDay = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month),
                EndDay = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month),
                BeginHour = 23,
                BeginMinute = 15,
                EndHour = 23,
                EndMinute = 45
            };

            monthlyTJ.Schedule = monthlyTJSchedule;
            monthlyTJ.Properties.Add("siteurl", site.Url);
            monthlyTJ.Update();
       }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job
        }

No comments:

Post a Comment