Tuesday, March 6, 2012

Part 1: How to develop custom timer job in sharepoint 2010 using visual 2010


Step 1: Open Visual Studio 2010 | New Project | Empty Sharepoint Project | its name TimerJob2010
Step 2: Choose “Deploy as farm solution”
Step 3: Add new Class, its name TimerJobClass.cs
Step 4: Inherit class from Microsoft.SharePoint.Administration.SPJobDefinition class. And code as
Code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace TimerJob2010
{
    class TimerJobClass : SPJobDefinition
    {
        public TimerJobClass()
            : base()
        {
        }
        public TimerJobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }
        public TimerJobClass(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, nullSPJobLockType.ContentDatabase)
        {
            this.Title = "Timer Job 2010";
        }
        public override void Execute(Guid contentDbId)
        {
            // get a reference to the current site collection's content database
            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
            // get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database
            SPList Listjob = contentDb.Sites[0].RootWeb.Lists["TimerJob2010"];
            // create a new list Item, set the Title to the current day/time, and update the item
            SPListItem newList = Listjob.Items.Add();
            newList["Title"] = DateTime.Now.ToString();
            newList.Update();
        }
    }
}
Step 5: Right click on Feartures | Add Feature
Step 6: You can rename Feature as
Step 7: Right click on Feartures | Add Event Receiver
Step 8: Code as
Code here
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace TimerJob2010.Features.TimerJobFeature
{
    /// <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("3bec3dd0-3cf3-419e-8c12-1adcbf7d2abc")]
    public class TimerJobFeatureEventReceiver : SPFeatureReceiver
    {
        const string ListTimerJob = "ListTimerJob";
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            // make sure the job isn't already registered
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == ListTimerJob)
                    job.Delete();
            }
            // install the job
            TimerJobClass listLoggerJob = new TimerJobClass(ListTimerJob, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;
            listLoggerJob.Schedule = schedule;
            listLoggerJob.Update();
        }
        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            // delete the job
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == ListTimerJob)
                    job.Delete();
            }
        }
    }
}

Step 9: Right click Feature | View Designer
Step 10: Change cope from Web to Site
Step 11: If cope incorrect, appear as
Step 12: Build project and Deploy it
Step 13: Go to Central Admin | Check job status
Step 14: You see custom timer job created, click to timer job and configure
Step 15: Configure time execute
Step 16: Open your web application | Open your  list and see item was created

1 comment:

  1. That in conjunction with the inexperience throughout meeting with regarding civilian job search engine, find jobs workable employment locating abilities is usually specifically influencing the flexibility of these veterans to locate work.

    ReplyDelete