Skip to main content

Running Timed Jobs within SharePoint


Programming a Timed Job


The Job installed in the aforementioned paragraph does not actually work because there is no "logic" to execute. To create a new Job, it is necessary to program a new class library and add it to the Global Assembly Cache (GAC) to make the logic accessible to SharePoint.
The first step is to create a new "Class Library" Visual Studio 2005 project with a suitable name, then add a reference to Windows SharePoint Services (Microsoft.SharePoint.dll) and "using" directives to Microsoft.SharePoint and Microsoft.SharePoint.Administration. The Class needs to inherit from "SPJobDefinition" and to have at least one constructor to initialize the new objects. The definition of the empty class reads:
using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CountDocs
{
   public class CountDocsJob : SPJobDefinition
   {
      public CountDocsJob()
      {
      }

      //public CountDocsJob(string MyJobName,
      //SPService MyService,
      //SPServer MyServer, SPJobLockType MyTargetType) :
      //base(MyJobName, MyService, MyServer, MyTargetType)
      //{
      //}

      public CountDocsJob(string MyJobName, SPWebApplication
         MyWebApplication) : base(MyJobName, MyWebApplication,
         null, SPJobLockType.Job)
      {
      }

      public override void Execute(Guid ContentDataBaseID)
      {
      }
   }
}
The base class has two constructors but if two are unnecessary, it is possible to define only one (the second one is commented in the example). The constructor of the sub-classing indicates that the third parameter (the SPServer parameter) is superfluous in this case.

blic override void Execute(Guid ContentDataBaseID)
The functional source code is written in the override "Execute" function. This function always has a parameter that receives the ID of the SharePoint's content DataBase that is executing the Job. For the example, the code reads the number of entries in a List and each time it runs, a new entry is created indicating the time and number of found entries:
{
   try
   {
      SPSite mySite = new SPSite("http://ServerName");
      SPWeb myWeb = mySite.OpenWeb();
      SPList myList = myWeb.Lists["MyJobs"];

      int intCounter = 0;
      foreach (SPListItem myItem in myList.Items)
         intCounter++;

      SPListItem newTask = myList.Items.Add();
      newTask["Title"] = "Counter of Items at " +
         DateTime.Now.ToString();
      newTask["Description"] = "At " + DateTime.Now.ToString() +
         " there are " + intCounter.ToString() +
         " items in the List";
      newTask.Update();
   }
   catch (Exception ex)
   {
      TextWriter myWriter =
         new StreamWriter(@"c:\CountDocsJobError.txt");
      myWriter.WriteLine(ex.ToString());
      myWriter.Close();
   }
}
In the case in point, the code makes a reference to the Site, Web, and List, counts the number of entries in the List, and creates a new entry. The names of the Site and List are fast-coded, but they can be configured in the web.config file of the WebApplication using code similar to this:
Configuration MyConfig =
   WebConfigurationManager.OpenWebConfiguration("/", "Sharepoint -80");
string myValue = myConfig.AppSettings.Settings["MyKeyName"].value;
or extracted from the "ContentDataBaseID" variable.
Sign the assembly with a Safe Name and install it in the GAC (obligatory). After resetting the IIS (iisreset), the previously discussed "CreateJob" routine can be modified to install the Job in the server:
static void CreateJob()
{
   SPServer myServer = SPServer.Local;
   SPSite mySite = new SPSite("http://ServerName");
   SPWebApplication myApplication = mySite.WebApplication;
   SPJobDefinitionCollection myJobsColl =
      myApplication.JobDefinitions;

   CountDocsJob myJob = new CountDocsJob("myCountDocsJob",
      myApplication);
   myJob.Title = "Docs Counter Job";

   SPMinuteSchedule myMinuteSchedule = new SPMinuteSchedule();
   myMinuteSchedule.BeginSecond = 0;
   myMinuteSchedule.EndSecond = 59;
   myMinuteSchedule.Interval = 1;
   myJob.Schedule = myMinuteSchedule;

   myJobsColl.Add(myJob);
   myApplication.Update();

   Console.WriteLine("Job Installed");
}

Comments

Popular posts from this blog

Migrating from Skype for Business to Microsoft Teams: A Step-by-Step Guide

Do you still use Skype for Business to meet the communication and collaboration needs of your business? If so, now is the perfect time to think about switching to Microsoft Teams, a cutting-edge platform with cutting-edge capabilities and seamless connectivity with other Microsoft services. But if you're unfamiliar with the procedure, switching to a new platform can seem like a difficult task. I'll walk you through the process of switching from Skype for Business to Microsoft Teams in this article. Plan the migration in Step 1 You must make a plan before you start the relocation procedure. Set a deadline, make a list of all the Skype for Business capabilities you presently use, and choose whether to migrate gradually or all at once. Step 2: Set up your surroundings. Make sure your network and infrastructure fulfil Microsoft Teams' standards. Upgrades to your hardware, software, and licences might be necessary. Additionally, confirm that you have the right permissions to ca

How to: Debug SharePoint Applications

You can greatly simplify debugging by using Visual Studio extensions for Windows SharePoint Services. This topic includes procedures that explain the following: Debugging with Visual Studio extensions for Windows SharePoint Services Performing manual debugging Performing remote debugging Debugging with Visual Studio extensions for Windows SharePoint Services Press the F5 key to begin debugging with Visual Studio extensions for Windows SharePoint Services. The following procedure demonstrates how to enable F5 debugging. To enable F5 debugging Locate and open the target SharePoint application's Web.config file. By default, it is located in C:\Inetpub\wwwroot\wss\VirtualDirectories\80. Find the following line of code and change the  debug  attribute to  true . Save the changes to the Web.config file. In Visual Studio, right-click the SharePoint project, and then click  Properties . Click the  Debug  tab, and then type the target SharePoint URL in t

SharePoint 2010 – How to use Audio and Video Webpart

Video and Audio Web Part [ a.k.a Media Web Part ] is one of the new cool web parts in SharePoint 2010, so in this article I am going to show you the way to add this web part in your page. To add Media web part to the page you must activate those two features 1- SharePoint Server Publishing Infrastructure Feature in Site Collection features 2- SharePoint Server Publishing Feature in Site features After that edit page then click on Insert tab in SharePoint Ribbon then select Video and Audio Now the Media Web Part in the page Click on the Media Web Part and you will find Options Tab in the Ribbon As you can see in the Options tab there are Chang Media button allow you to select the video and Chang Image button to change the image of web part and Styles (Dark, Light) Click on Change Media to select the Media file. From Computer opens up the Upload media so you can upload the video and display it. Once you click ok and saving the page. Click on play button and enj