1
votes

How do I install custom SharePoint Timer job on MOSS

Hello I created, custom SharePoint Timer job, Below in this document is my code.

I wrote and installed a lot of webparts.

But this is the first time I write SharePoint Timer job on MOSS.

i try to deploy it wite wspBuilder, try to copy it to gac, But the JOB not appear on the list JOB on Central Administration site , Central Administration > Operations > Timer Job Definitions ,
how can i add it and see it on the ,

Central Administration > Operations > Timer Job Definitions the code.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint;
namespace WSPBuilderProject1.FeatureCode
{
    public class MyTimerJob : SPJobDefinition
    {
        public MyTimerJob()
            : base()
        {
            this.Title = "My Timer Job";
        }

        public MyTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
            this.Title = "My Timer Job";
        }

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


        public override void Execute(Guid contentDbId)
        {

            using (SPWeb oWeb = SPContext.Current.Site.OpenWeb("/"))
            {
                SPWeb mySite = SPContext.Current.Web;
                mySite.AllowUnsafeUpdates = true;
                SPListItemCollection listItems = mySite.Lists["AuditLogCalculatedData"].Items;
                int itemCount = listItems.Count;

                for (int k = 0; k < itemCount; k++)
                {
                    try
                    {
                        listItems.Delete(k);
                    }
                    catch { }
                }


                    SPListItem item = listItems.Add();

                    item["FileName"] = "roi";
                    item["NumOfEntries"] = 10102;


                    item.Update();

                mySite.AllowUnsafeUpdates = false;

            }

        }
    }
}
2
Can you tell use the issues you are facing ...Madhur Ahuja
Webpart is an UI element. TimerJobs doesn't show any interface. What exactly do you want to achieve? What do you want your timer job to do?Janis Veinbergs
Please specify the sharepoint version.leppie

2 Answers

1
votes

This is the sample code which can be executed on FeatureActivated event

public override void FeatureActivated (SPFeatureReceiverProperties props) {
  SPWebApplication webApp = props.Feature.Parent as SPWebApplication;
  if (webApp == null)
    throw new SPException("Error obtaining reference to Web application.");

  // Ensure the job is not already registered.
  foreach (SPJobDefinition job in webApp.JobDefinitions)
    if (job.Name == JOB_NAME) job.Delete();

  // Install job.
  SharePointWarmupJob warmupJob = new SharePointWarmupJob(webApplication);

  // Schedule the job to run every minute all the time.
  SPMinuteSchedule schedule = new SPMinuteSchedule();
  schedule.BeginSecond = 0;
  schedule.EndSecond = 59;
  schedule.Interval = 1;
  warmupJob.Schedule = schedule;

  // Save changes.
  warmupJob.Update();
}
0
votes

The solution

If you read down the article, there is a zipped solution file (TaskLogger_CustomTimerJob.zip) which you can use and compile yourself.

VS2008 will prompt you to convert project to 2008, just do it and compile.

Building the project

The problem you can't build lies within file BuildSharePointPackage.Targets. MakeCabPath is not valid for your environment. My makecab.exe lies in C:\Windows\system32 path. I tried to change path and rebuild, but it didn't work, it somehow tried to use the old location. So i just copied makecab.exe to C:\Program Files\Microsoft Cabinet SDK\bin and it builds sucessfully.

WSPBuilder

Also, you may want to use WSPBuilder to alleviate the pain on creating SharePoint solutions. It installs visual studio extension which you can use to make wsp file and deploy from visual studio. Download WSPBuilder here, and use Walkthrough of the Visual Studio Add-in