Ads

Wednesday, 27 March 2013

How to make a Timer Job Feature for SharePoint 2010


Timer Job Feature is a task or function that can run base with scheduled on Munites, Hoursly, Daily, Weekly and Monthly
First at all, we see what is Timer Job Feature look like on SharePoint 2010

Minutes
Hourly

Weekly

Monthly
Data that added to Task SPList if we set timer job scheduled runs each 5 minutes
Now let start to make a Timer Job Feature
1. Start -> all Programs -> Microsoft Visual Studio 2010 -> Microsoft Visual Studio 2010
2. File -> New -> Project...
3.  Visual C# -> SharePoint -> 2010 -> Empty SharePoint Project -> OK
4. Choose "Deploy as a farm solution"
5. Add new timer job class by right click on project name -> Click "Add" -> Click "New Item..."
6. Visual C# -> Code -> Class -> TimerJobFeatureA.cs -> Add
7.  TimerJobFeatureA.cs content

8. Full code of class TimerJobFeature.cs / Code của lớp TimerJobFeature.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace TimerJobFeatureA
{
    class TimerJobFeatureA : Microsoft.SharePoint.Administration.SPJobDefinition
    {
        public TimerJobFeatureA()
            : base()
        { }

        public TimerJobFeatureA(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        { }

        public TimerJobFeatureA(string jobName, SPWebApplication webApplication, SPServer server, SPJobLockType targetType)
            : base(jobName, webApplication, server, targetType)
        { }

        public TimerJobFeatureA(string jobName, SPWebApplication webApplication)   
                : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {   
            this.Title = "Timer Job Feature A";   
        }

        /// <summary>
        /// Implement code for our task job here
        /// </summary>
        /// <param name="targetInstanceId"></param>
        public override void Execute(Guid targetInstanceId)
        {
            // get a reference to the current site collection's content database
            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
            if (contentDb == null)
                return;
            if (contentDb.Sites.Count <= 0)
                return;          
            // get a reference to the "Task" list in the RootWeb of the first site collection in the content database           
            SPList Listjob = contentDb.Sites[0].RootWeb.Lists.TryGetList("Task");
            if (Listjob == null)
                return;
            // 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();
        }
    }
}

9.  Right click Features -> Click Add Feature
10. Change feature name to TimerJobFeatureA
11. Feature name after changed
12.  Add Event Receiver for feature
13. TimerJobFeatureA.EventReceiver.cs
14. Code of TimerJobFeatureA.EventReceiver.cs
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace TimerJobFeatureA
{
    /// <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("7279cf5a-f7fd-4848-afe5-c4387da743f3")]
    public class TimerJobFeatureAEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        const string List_JOB_NAME = "TimerJobFeatureA";
        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 == List_JOB_NAME)
                    job.Delete();
            }
            // install the job   
            TimerJobFeatureA listLoggerJob = new TimerJobFeatureA(List_JOB_NAME, 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 == List_JOB_NAME)
                    job.Delete();
            }
        }       
    }
}

15. Click View Designer of feature
16.  Change Scop from Web to Site


17.Click to show properties of project
18. Type your sharepoint server location
19. Deploy the TimerJobFeatureA to your sharepoint server
20. Deploy succeeded
21. Start -> all Programs -> Microsoft SharePoint 2010 Products -> SharePoint 2010 Central Administration /
22. Check job status
23.  Browse to see Timer Job Feature A then click on it / Click on icon >
24. Can be set scheduled for Timer Job


No comments:

Post a Comment

Ads