Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

asp.net error page on Feature Activation

$
0
0

I'm having a problem activating a feature I have written and installed.
This feature should create a timer job when activated, the timer job simply queries a list and sends emails where applicable.

I am sure my problem is down to scope but I am not sure how to change it or what it should be.
The code (under execute) is hard coded to point to a single list therefore this feature and timer job reference this single list only. It is not necessary to have the activate feature button anywhere.

However: I have tried scoping the feature to both web and webapplication level and get the same problem.
Here is my feature activated code:

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

        public bool CreateJob(SPWebApplication site)
        {
            bool jobCreated = false;

            try
            {
                Reminders job = new Reminders(JobName, site);
                SPMinuteSchedule schedule = new SPMinuteSchedule();
                schedule.BeginSecond = 0;
                schedule.EndSecond = 59;
                schedule.Interval = 15;
                job.Schedule = schedule;
                job.Update();
            }
            catch (Exception)
            {

                throw;
            }
            return jobCreated;
        }

        public bool DeleteJob(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;
        } 
The error I see in the ULS logs when I click the activate feature button (under site features (not site collection of web app)) is:

System.InvalidCastException: Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.Administration.SPWebApplication'.  

at IncidentReminders.Features.IncidentReminders.IncidentRemindersEventReceiver.<>c__DisplayClass2.<FeatureActivated>b__0()   


I am using: install-spsolution -identity IncidentReminders.wsp -gacdeployment and then restarting the timer service to make sure it also gets deployed to the app server.

Can someone see where I have gone wrong?
Once I have the code sorted I will look at making this a hidden feature as it's only purpose is to create a timer job and has no place being in the list of site feature buttons on every site in the web app


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>