Tuesday 3 December 2013

Timer Job Example in SharePoint 2013

Share it Please

1.open visual studio 2012
2.Add SharePoint2013-Empty Project named it "TimerJobExample"
3.Enter your site collection and choose Deploy as a Farm solution
4.Add class file in your solution named it "Employee"
5.paste the below code inside the Employee class

 public class Employee : SPJobDefinition
    {
        public Employee()
            : base()
        {
            Title = "Sithik Job";

        }

        public const string JobName = "Sithik Job";
        public Employee(SPWebApplication webApp) :
            base(JobName, webApp, null, SPJobLockType.Job) { }
        public override void Execute(Guid targetInstanceId)
        {
            AddNewItem();

        }
  public void AddNewItem()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList listEmpInsert = web.Lists["Employee"];
                        web.AllowUnsafeUpdates = true;
                        SPListItem EmpInsert = listEmpInsert.Items.Add();
                        EmpInsert["EmpName"] = "Mohamed";
                        EmpInsert["Age"] = "28";
                        EmpInsert["Address"] = "Chennnai";
                        EmpInsert.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }  

    }
6.Go to Features right click add one new features and rename it "Employee"
7.Right click the Employee Features  add one "Event receiver" paste the below code inside the receiver

 private void DeleteJob(Microsoft.SharePoint.Administration.SPJobDefinitionCollection jobs)
        {
            foreach (SPJobDefinition job in jobs)
            {
                if (job.Name.Equals(TimerJobExample.Employee.JobName,
                StringComparison.OrdinalIgnoreCase))
                {
                    job.Delete();
                }
            }
        }

   public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);


            TimerJobExample.Employee simpleJob = new TimerJobExample.Employee(webApp);

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

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

      public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);
        }

8.click the "Employee Features" Change the scope to "WebApplication"
9.click the "properties windows" "Always Force to Install" to "True"
10.Deploy the project
11.Go "central administration" click "Monitoring"
12.under "Timer Jobs" click "Review Job definitions"
13.you will see your Timer job named "Sithik Job"



No comments:

Post a Comment

Followers

Follow The Author