Monday 20 February 2017

Error occurred in deployment step 'Activate Features': Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.Administration.SPWebApplication'

Hi Friends,

    I am getting following error, Please help me.

Severity Code Description Project File Line Suppression State
Error Error occurred in deployment step 'Activate Features': Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.Administration.SPWebApplication'. SP13TimerJob 0

My Source Code :

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

namespace SP13TimerJob.Features.TimerJobFeature
{
    ///

    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    ///
    ///
    /// The GUID attached to this class may be used during packaging and should not be modified.
    ///

    [Guid("2c4db5bb-eb8a-4134-af07-2cd5050ffb5d")]
    public class TimerJobFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        const string JobName = "Demo Job";
        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;
            }
        }
        private bool CreateJob(SPWebApplication site)
        {
            bool jobCreated = false;
            try
            {
                TimerJobDemo job = new TimerJobDemo(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;
        }

        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;
                }
            }
        }
     
    }
}


anyone can please help me ?