Saturday, August 4, 2012

Creating and deploying the timer jobs in share point 2010


When there is any activity wants to happen automatically in share point then there is one quick solution and that is Timer job.Timer job runs in a specific Windows service for SharePoint Server.Its very easy to create the timer job and deploy to share point server.Timer job can be set from seconds to years period.Timer job contains a definition of the service to run and specifies how frequently the service is started. SharePoint timer service runs the the timer job.This feature is debugged uisng OWSTimer process.

Below is one example which gives idea of creating and deploying the timer job. It definately help you in enhansing your knoweldgebase.

Steps for creating the timer job.

1. Create new project from visual studio with share point template




2.Once the project is selected now its time to to set the SP server url. Since the timer job can not be deployed as sandbox solutions so select the farm solution for deployment.




3. Open the solution explorer and right click on project name and add the new item and that is class.



4. After adding the class now add the feature because the timer is deployed as feature to share point server. Right click on feature and select add feature and sets its scope.



5. Add the event receiver by right clicking on the feature.

6. Once all the required files are added in solution now its time to play with code. So open the class which we added in step 3 and add code as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace TestTimerJob
{
    public class TestTimerJob : SPJobDefinition
    {
        public TestTimerJob()

            : base()
        {

        }

        public TestTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)

            : base(jobName, service, server, targetType)
        {

        }

        public TestTimerJob(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {

            this.Title = "Test Timer Job";

        }
        public override void Execute(Guid contentDbId)
        {

            SPSite oSPSite = new SPSite("http://MySharepointTestServer");
            SPWeb oSPWeb = oSPSite.RootWeb;
            oSPWeb.AllowUnsafeUpdates = true;

            SPList testList = oSPWeb.Lists["TestList"];
            SPListItem newTaskItem = testList.Items.Add();
            newTaskItem["Title"] = "NewItem";
            newTaskItem.Update();

            oSPWeb.AllowUnsafeUpdates = false;
        }
    }
}


6.  Now add the code for event receiver  class for its scheduler logic, activation and deactivation method as like below.

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace Test.Features.Feature1
{
    ///
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    ///
    ///
    /// The GUID attached to this class may be used during packaging and should not be modified.
    ///

    [Guid("1d48fa2f-edce-428c-bfea-bb9fc473f9ca")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        const string List_JOB_NAME = "TestListTimer";
        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

            TestTimerJob listLoggerJob = new TestTimerJob(List_JOB_NAME, site.WebApplication);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            //SPHourlySchedule schedule1 = new SPHourlySchedule();
            //SPDailySchedule sc = new SPDailySchedule();
            //SPYearlySchedule scy = new SPYearlySchedule();
            
        

            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();

            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgradin8. g.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary parameters)
        //{
        //}
    }
}



7. Build the solution and deploy to the server.


8. The above deployment can be done on VM or your development server but if you have to deploy it to the production then you have to use STSADM or PoweShell command.

shell command” - Enable-SPFeature -Identity “FeatureName” -Url “https://Servername″

STSADM  - stsadm -o activatefeature -fileName MyTestTimer\TestTimerFeature.xml -url  -force

                  stsadm -o deactivatefeature -fileName MyTestTimer\TestTimerFeature.xml -url   -force





No comments: