locked
Unable to delete Timer job RRS feed

  • Question

  • Hi,

    Below is my Timer job def

    namespace ddicorp
    {
        class EmailRequestTimerJob : SPJobDefinition
        {
            public const string Job_Name = "EmailRequstTimerJob";
    
            //over ride the necessary constructors
            public EmailRequestTimerJob(): base(){ }
    
            public EmailRequestTimerJob(SPWebApplication webApp)
                :base(Job_Name, webApp, null, SPJobLockType.ContentDatabase)
            {
                //assign the title
                this.Title = "Email Request Timer Job";
            }
    
            //override the execute method
            public override void  Execute(Guid contentDBID)
            {
     	         //include code to process records from SendEmail list where MailSent is false 
                SPWebApplication webApp = this.Parent as SPWebApplication;
                SPContentDatabase contentDB = WebApplication.ContentDatabases[contentDBID];
    
                //SPWeb currentWeb = SPContext.Current.Web;
    
                SPList list = contentDB.Sites[0].RootWeb.Lists["SendEmail"];
                SPQuery query = new SPQuery(list.DefaultView);
    
                query.Query = string.Concat("<Where>",
                      "<Eq><FieldRef Name='MailSent'/>",
                      "<Value Type='Boolean'>1</Value></Eq></Where>"                  
                      );
    
                SPListItemCollection items = list.GetItems(query);
    
                if (items.Count != 0)
                {
                    foreach (SPListItem item in items)
                    {
                        //invoke logic to trigger emails
                        if (SendEmail(item["fromEmail"].ToString(), item["ToEmailAddress"].ToString(), item["EmailText"].ToString(), contentDB.Sites[0].RootWeb))
                        {    //update the list item's MailSent field
                            item["MailSent"] = "True";
                            item.Update();
                        }  
                    }
                }
            }
    
            protected bool SendEmail(string fromAddress, string toAddress, string msgBody, SPWeb web)
            {
                try
                {
                    var headers = new StringDictionary();
                    headers.Add("subject", "Site Email Request");
                    headers.Add("content-type", "text/html");
                    headers.Add("to", toAddress);
                    headers.Add("from", fromAddress);
                    return SPUtility.SendEmail(web, headers, msgBody);
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    
    

    This is the feature activation code

    const string job_Name = "EmailRequstTimerJob";
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                //create the timer job when the feature is activated
                SPSite site = properties.Feature.Parent as SPSite;
    
                //delete the job if its already created
                foreach (SPJobDefinition jd in site.WebApplication.JobDefinitions)
                {
                    if (jd.Name == job_Name)
                    {
                        jd.Delete();
                        break;
                    }
                }
    
                //create the timer job by creating the instance of the EmailRequestTimerjob.cs
                EmailRequestTimerJob customTimerJob = new EmailRequestTimerJob(site.WebApplication);
    
                //create a schedule for timer job
                SPHourlySchedule customSchedule = new SPHourlySchedule();
                customSchedule.BeginMinute = 0;
                customSchedule.EndMinute = 59;
                //customSchedule.interval= 1;
    
                //assign the schedule tothe timer job
                customTimerJob.Schedule = customSchedule;
                customTimerJob.Update();
    
            }

    I get Error on jd.Delete(), Have looked into the log can see 3 different error messages related to the timer job listed below:

    SharePoint cannot deserialize an object of type ddicorp.SendEmailRequestTimerJob.EmailRequestTimerJob, ddicorp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d1c071c04759ddd on this machine.  This typically occurs because the assembly containing this type is not installed on this machine.  In this case, this message can be safely ignored.  Otherwise, the assembly needs to be installed on this machine in a location that can be discovered by the .NET Framework.

    The type ddicorp.SendEmailRequestTimerJob.EmailRequestTimerJob, ddicorp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d1c071c04759ddd could not be found in its specified assembly.  Scanning all assemblies that have been loaded in the current app domain.

    but the timer job is listed in the central admin, the feature is scoped at site and I have just a root site in a sitecollection. how do I go about deleting the timer job? I tried using powershell but no use looks like I have messed up the deployment.

    Thanks,


    san

    Tuesday, August 14, 2012 10:47 AM

Answers

  • Hi

    Are you using the Powershell  cmdlets in the prescribed order?  Do you run Uninstall-SPSolution before Remove-SPSolution ?  Also when installing, did you use the -GACDeployment  argument ? "Assembly not found" may indicate that thare are some .dll's missing in the GAC.

    Restarting the Timer Service is recommended when reinstalling, as already mentioned.


    Kind Regards Bjoern
    Blog


    • Edited by Bjoern H Rapp Tuesday, August 14, 2012 4:32 PM
    • Marked as answer by Lhan Han Thursday, August 23, 2012 9:29 AM
    Tuesday, August 14, 2012 4:29 PM

All replies

  • Did you make changes to the namespace or class name and redeploy ? If you deploy a new assembly make sure to restart the SharePoint Timer Service to pick it up.

    Should your TimerJob class be "public" ?

    Tuesday, August 14, 2012 10:51 AM
  • Yes, I had renamed the namespace in the timerjob class, had not restarted the timer service will give it a try.

    thanks,


    san

    Tuesday, August 14, 2012 10:59 AM
  • now I don't see the above errors in the log, how ever on invoking the timer job breakpoint is not hit :( I have deployed the wsp from visual studio compiled in debug mode then attachedOWSTimer process from debug, Open the timer job def on central admin and clicked on run now

    tried the below ps script to invoke the job, nothing happens :(

    Get-SPTimerJob "EmailRequestTimerJob" -WebApplication "http://ddivsps21:7818 " | Start-SPTimerJob

    log does not show any error messages, the normal messages listing the solution deployment, activating of individual features. Any suggestions please?


    san

    Tuesday, August 14, 2012 12:29 PM
  • But that's the "old" timer job with the "old" namespace, right ? Remove that one and add the job with the correct namespace.

    Tuesday, August 14, 2012 1:11 PM
  • nope earlier it was SendEmailRequestTimerJob, then I had renamed it to EmailRequestTimerJob but I had not restarted the Timer job after rename.

    Regards,


    san

    Tuesday, August 14, 2012 3:27 PM
  • Hi

    Are you using the Powershell  cmdlets in the prescribed order?  Do you run Uninstall-SPSolution before Remove-SPSolution ?  Also when installing, did you use the -GACDeployment  argument ? "Assembly not found" may indicate that thare are some .dll's missing in the GAC.

    Restarting the Timer Service is recommended when reinstalling, as already mentioned.


    Kind Regards Bjoern
    Blog


    • Edited by Bjoern H Rapp Tuesday, August 14, 2012 4:32 PM
    • Marked as answer by Lhan Han Thursday, August 23, 2012 9:29 AM
    Tuesday, August 14, 2012 4:29 PM