Thursday, 19 July 2012

Timerjob

public Monthlysendemail2(): base()
        {
        }
        public Monthlysendemail2(string jobNamenew, SPService servicenew, SPServer servernew, SPJobLockType targetTypenew)
         : base(jobNamenew, servicenew, servernew, targetTypenew)
        {
        }
         public Monthlysendemail2(string jobNamenew, SPWebApplication webApplication)
        : base(jobNamenew, webApplication, null, SPJobLockType.ContentDatabase)
        {
          this.Title ="MEMC_RMT_Employee2ndAlert";
        }
         public override void Execute(Guid contentDbId)
         {
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 SPWebApplication webApplication = this.Parent as SPWebApplication;
                 SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
                 using (SPSite ositeval = new SPSite(contentDb.Sites[0].ID))
                 {
                      using (SPWeb oweb = ositeval.RootWeb)
                     {
                         SPList olist = oweb.Lists.TryGetList("TimeSheet Approvers");
                         if (olist != null)
                         {
                             SPQuery qry = new SPQuery();
                             qry.Query = "<Where><And><Eq><FieldRef Name='Employee_x0020_Status' /><Value Type='Choice'>active</Value></Eq><Eq><FieldRef Name='Status' /><Value Type='Text'>Not Started</Value></Eq></And></Where>";
                             SPListItemCollection oitemcoll = olist.GetItems(qry);
                             if (oitemcoll.Count > 0)
                             {
                                 foreach (SPListItem oitem in oitemcoll)
                                 {
                                     SPFieldUserValue userval = new SPFieldUserValue(oweb, oitem["Employee"].ToString());
                                     SPFieldUserValue approveremail = new SPFieldUserValue(oweb,oitem["Approver"].ToString());
                                     SPUser ouser = userval.User;
                                     SPUser appuser = approveremail.User;
                                     string appemailid = appuser.Email;
                                     if (!string.IsNullOrEmpty(appemailid))
                                     {
                                      //send email  to the approver
                                      sendemailforapprover(appemailid,oweb,ouser);
                                     }
                                     string emailid = ouser.Email;
                                     if (!string.IsNullOrEmpty(emailid))
                                     {
                                       //send email to the employee's email id
                                       sendemail(emailid, oweb);
                                     }
                                     SPGroup hrgrp = oweb.SiteGroups["HR RMT Group "];
                                     SPUserCollection hruserscoll = hrgrp.Users;
                                     if (hruserscoll.Count > 0)
                                     {
                                         foreach (SPUser ouserhr in hruserscoll)
                                         {
                                            string hremail = ouserhr.Email;
                                            sendemailforhr(hremail, oweb,ouserhr.Name.ToString());
                                         }
                                     }

                                 }
                             }
                         }
                    }
                  }
               });
            }

Feature

 public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.


        //this is the jobname so that only two different timerjob for the same site can be deployed.
        const string List_JOB_NAME_Val ="MEMC_RMT_Employee2ndAlert";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite osite = properties.Feature.Parent as SPSite;


            // make sure the job isn't already registered
            foreach (SPJobDefinition job in osite.WebApplication.JobDefinitions)
            {
               if (job.Name == List_JOB_NAME_Val)
               job.Delete();
            }
            Monthlysendemail2 listLoggerJob = new Monthlysendemail2(List_JOB_NAME_Val, osite.WebApplication);
            SPDailySchedule dailymail = new SPDailySchedule();
            dailymail.BeginHour = 11;
            dailymail.BeginMinute = 0;
            dailymail.BeginSecond = 0;
            dailymail.EndHour = 11;
            dailymail.EndMinute = 00;
            dailymail.EndSecond = 01;
            listLoggerJob.Schedule = dailymail;
            listLoggerJob.Update();


           }
           
        
        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_Val)
              job.Delete();


            }
        }


No comments:

Post a Comment