Wednesday 29 January 2014

Creating Custom Timer Job in SharePoint

Steps
Description
Step 1
Creation timerjob solution
Step 2
Adding Feature
Step 3
Activating the timerjob feature
Step 4
Running Timer Job
Step 5
Debugging timerjob


Step 1:  Creation Timer job solution

Select “Empty SharePoint Project”  and click OK

Select “Deploy as a Farm Solution” and click Finish

Add class file

For Excel component, add the interop reference

Add Reference as WindowsBase

Include the below references and code in the class file. Replace “SampleTimerJob_Class” with your class filename.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Configuration;
using System.Data;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using Microsoft.SharePoint.Utilities;

namespace SampleTimerJob
{
    public class SampleTimerJob_Class : SPJobDefinition
    {
         public SampleTimerJob_Class() : base() { }
         public SampleTimerJob_Class(string jobName, SPWebApplication webApp)
            : base(jobName, webApp, null, SPJobLockType.Job)
        {
            this.Title = "OTS Journal Timer Job"; //This will be your timerjob name
        }

                string SiteUrl = string.Empty;
         public override void Execute(Guid targetInstanceId)
         {
            SiteUrl = Convert.ToString(this.Properties["SiteCollectionUrl"]);
               //Here is your code starts
         }

    }
}

Step 2 : Adding Feature


Right click on “Feature” and Click “Add Feature”

This title will be appeared in “Site Collection Features”, Select “Site” in Scope

Save the feature

Add the event receiver
Add the namespace
using Microsoft.SharePoint.Administration;

Replace this method by the below code and do the necessary changes

public class Feature1EventReceiver : SPFeatureReceiver
    {
        SPSite site = null;
        SPWeb web = null;
        public Feature1EventReceiver()
        {
            // string siteUrl = "http://balaj8t-asus/";
//string siteUrl = "http://siva-pc/"; //This will not work, if it is given Config. Hard //code here

            //SPSecurity.RunWithElevatedPrivileges(delegate()
            //{
            //    using (site = new SPSite(siteUrl))
            //    {
            //        web = site.OpenWeb();
            //    }
            //});
        }
        // Uncomment the method below to handle the event raised after a feature has been //activated.
        const string JobName = "Journal Timer Job";


        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            site = properties.Feature.Parent as SPSite;

            // make sure the job isn't already registered

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {

                if (job.Name == JobName)

                    job.Delete();

            }

            // install the job

            SampleTimerJob_Class listLoggerJob = new SampleTimerJob_Class(JobName, site.WebApplication);

           //Adding property for to find Site URL
                     listLoggerJob.Properties.Add("SiteCollectionUrl",site.Url);

            SPMinuteSchedule schedule = new SPMinuteSchedule();

            schedule.BeginSecond = 0;

            schedule.EndSecond = 59;

            schedule.Interval = 5;

            listLoggerJob.Schedule = schedule;

            listLoggerJob.Update();


        }

        private static void DeleteJob(SPSite site)
        {
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                if (job.Name == JobName)
                    job.Delete();
        }
        private static void CreateJob(SPSite site)
        {
            SampleTimerJob_Class job = new SampleTimerJob_Class(JobName, site.WebApplication);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 5;
            schedule.Interval = 5;

            job.Schedule = schedule;
            job.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job
        }


    }



Step 3 : Activating the Timer job feature


Go to central admin and click on “Site Actions>>Site Settings”

Click on “Site Collection Features”

Click on Activate

Step 4 : Running Timer Job


Click on “Monitoring” after that click on “Review Job Definitions”

Click on “Sample Timer Job”

Can do all the settings and click “Run Now”

Step 5 : Debugging Timer job

Click on “Attach to Process”

Select “OwsTimer.EXE” and click on “Attach”

After attaching click on “Run Now”

The breakpoint fires.

After debugging, click on “Detach All”













No comments:

Post a Comment