1

I am creating a Timer Job in SharePoint 2013 using the below mentioned links:

My solution is getting deployed and I can activate/deactivate feature from Central Admin. But the timer job is not appearing in CA.

When I attached the debugger at the Timer Job's class file, it gets goes only to the first constructor. i.e. at the non parametrized constructor.

The debugger does not go to the other 2 constructors.

Moreover, when I try to debug the Feature's event receiver the debugger does not stops at the code, it only stops at the Timer Job's class file.

I am doing it with all the administrator rights.

My Event Receiver Code is :

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SharePointProj4.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("6f5a5ed1-98d3-4cde-a497-deef1b3b1467")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        const string JobName = "SP Task Timer";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
                    SPSite site = properties.Feature.Parent as SPSite;
                    DeleteExistingJob(JobName, parentWebApp);
                    CreateJob(parentWebApp);
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            lock (this)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
                        DeleteExistingJob(JobName, parentWebApp);
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


        private bool CreateJob(SPWebApplication site)
        {
            bool jobCreated = false;
            try
            {
                SPJobs.SPTImerJobClass job = new SPJobs.SPTImerJobClass(JobName, site, "http://win-njfp7te48bn/");
                //SPJobs.SPTImerJobClass job = new SPJobs.SPTImerJobClass(JobName, site);
                SPMinuteSchedule schedule = new SPMinuteSchedule();
                schedule.BeginSecond = 0;
                schedule.EndSecond = 59;
                schedule.Interval = 15;
                job.Schedule = schedule;

                job.Update();
            }
            catch (Exception)
            {
                return jobCreated;
            }
            return jobCreated;
        }
        public bool DeleteExistingJob(string jobName, SPWebApplication site)
        {
            bool jobDeleted = false;
            try
            {
                foreach (SPJobDefinition job in site.JobDefinitions)
                {
                    if (job.Name == jobName)
                    {
                        job.Delete();
                        jobDeleted = true;
                    }
                }
            }
            catch (Exception)
            {
                return jobDeleted;
            }
            return jobDeleted;
        }

        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

and my TimerJob Code is:

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

namespace SharePointProj4.SPJobs
{
    class SPTImerJobClass : SPJobDefinition
    {
        public SPTImerJobClass() : base() { }

        public SPTImerJobClass(string jobName, SPService service) : base(jobName, service, null, SPJobLockType.None)
        {
            this.Title = jobName;
        }

        public SPTImerJobClass(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["Tasks1"];
            SPListItem newTask = tasklist.Items.Add();
            newTask["Title"] = "New Task" + DateTime.Now.ToString();
            newTask.Update();
        }
    }
}
  • Is this a multiple server farm? – Evariste Sep 7 '15 at 11:02
  • No it is a single server Farm. – user31823 Sep 7 '15 at 11:08
  • Did you change the DeploymentServerType of the solution package (in Visual Studio) at any time? – Evariste Sep 7 '15 at 11:45
  • first of all, you can remove the "RunWithElevatedPrivileges". As you have to be farm admin in order to activate WebApplicationFeatures you don´t Need to elevate Rights. Additionally in your case it does not have any effect, because you are using the object from the functio´s Parameter, so you use also the security context given by it. Furthermore, runwithelevatedprivilges runs the code under the context of the current webapppool, and that´s not necessary the same user the requested Apppool runs with (especially CA-AppPool and "Frontend"-WebAppPool) – Leopold Lerch Sep 7 '15 at 12:24
  • did you create a custom constructor with 3 params? – Leopold Lerch Sep 7 '15 at 12:34
0

You don't have constructor with 3 parameters, but you call it. Ones which can work is commented out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.