Job Timer FeatureActivated dhe Fushëveprimi Feature

Unë kam qenë duke punuar me disa kodin që dikush dorëzuar për mua një punë timer.  Ai nuk kishte dhënë aktuale aktivizimin kodin tipar kështu që unë kam për të shkruar atë, natyrisht.  I took advantage of Andrew Connell’s famous blog post on the subject.

Unë jam duke përdorur Visual Studio 2010 and deployment kept failing with an error “Error occurred in deployment step ‘Add Solution’: Object reference not set to an instance of an object.”

I was taking his code too literally.  I was scoping the feature to the web application level, siç tregohet:

image

Si rezultat, the properties that are sent to the receiver are from the web application, not a site collection.  Në fund, the code looks like this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    // Don’t do the following with web app scoped features, it leads to despair buzëqeshje
    // SPSite site = properties.Feature.Parent as SPSite;

    SPWebApplication wa = properties.Feature.Parent as SPWebApplication;

    nëse (wa == null) throw new Exception("webapp2 is null.");

    foreach (SPJobDefinition job in wa.JobDefinitions)
    {

        mundohem
        {
            nëse (job.Name == List_JOB_NAME)

                job.Delete();
        }
        kap (Exception e)
        {
            throw new Exception("marker 2");
        } // catch exception e
    }

    // install the job

    WeatherForecastTimerJob weatherForecastTimerJob =
        new WeatherForecastTimerJob(List_JOB_NAME, wa);

    SPMinuteSchedule schedule = new SPMinuteSchedule();
    schedule.BeginSecond = 0;
    schedule.EndSecond = 59;
    schedule.Interval = 5;
    weatherForecastTimerJob.Schedule = schedule;
    weatherForecastTimerJob.Update();

}

The key take-away is that when the feature is scoped to a web app, the SPFeatureReceiverProperties that SharePoint passes to your feature receiver has web app level parameters.  Andrew’s old blog entry assumes it’s scoped to the site collection.

</fund>

Abonohen në blogun tim.

Atëherë ejani pas meje në Twitter në http://www.twitter.com/pagalvin

Lini një Përgjigju

Adresa juaj e emailit nuk do të publikohet. Fusha e kërkuar janë shënuar *