Program and Employ Windows Services for Microsoft SharePoint 2007

Windows SharePoint Services (WSS) and Microsoft Office SharePoint Services (MOSS) 2007 are Microsoft’s innovative tools that allow for collaboration and communication within organizations, enabling the smooth flow of documents and information. SharePoint 2007 has an extended Application Programming Interface (API) and an Object Model that reaches most parts of the system, allowing for greater program functionality not present in SharePoint’s default system.

Windows Services are applications that launch automatically at the moment of booting and operate in the background as long as Windows runs. They are useful to create long-running, independent applications without user interaction. Because they require a major part of the physical resources of the servers, sometimes it is necessary to execute SharePoint processes in the background; Windows Services ensure these tasks function in periods of low-user load. Windows Services can also manage SharePoint activities because they run independently of WSS (and MOSS), depending exclusively on the Windows infrastructure.

Windows Services and WSS Timed Jobs

Windows Services utilize their own Windows sessions and security context, isolating them from other processes and users working on the same server. Although Windows Services has no user interface, there is an administrator function to start, pause, restart, and realize the necessary configurations. SharePoint also has an infrastructure to operate this type of process in the form of “SharePoint Timed Jobs.” Jobs perform actions at predefined intervals (in minutes) or during an allocated period of time (hour, day, week, month, or year). Among other functions, SharePoint utilizes Timed Jobs for installing Solutions, indexing, and with its search engine.

One disadvantage of Timed Jobs is that the time structure is organized in defined slots (hour, days, and so forth). If a task requires a specified time configuration, for example, to run each second Saturday of the month, the Jobs framework falls short. Another drawback is that Timed Jobs are SharePoint-dependent; imagine, for instance, a system that needs to track the availability of the Portal. In this scenario, if SharePoint is down, the Jobs infrastructurealso is unavailable for use. A Windows Service that runs autonomously is able to check the accessibility every few minutes, and if necessary, create a log.

Programming Windows Services for SharePoint

Services are a special type of application and function differently from other Windows software. One difference is that the compiled executable does not operate with the default Windows double-click, but needs to be installed using special tools. Debugging presents a special challenge for the developer and the interaction with users needs to be carefully prepared because dialogue boxes and other types of messaging may interrupt the Service.

Prior to the introduction of DotNet Windows Framework, it was possible only to program Windows Services using unmanaged code (C++, among other languages), but with the arrival of Visual Studio 2005, CSharp, Visual Basic, and managed code can be used. DotNet 2.0 (and up) provides the Classes, Methods, and Properties needed to create and interface with Windows Services. The Syste.ServiceProcess.ServiceBase NameSpace supplies the events, methods, and properties essential to run a Service. The System.Configuration.Install.Installer and System.ServiceProcess.ServiceProcessInstaller NameSpaces comprise the classes to install and initialize the Service. Finally, the System.ServiceProcess.ServiceController NameSpace takes care of the communication with external applications.

The following Windows Service example was created to scan the Portal structure every 24 hours searching for Sites, Webs, Lists, Libraries, and the documents in the Libraries. The process is a time-consuming and resources-unfriendly job for SharePoint servers if the installation is extensive, and the server response could be considerably degraded. For this reason, it’s desirable to run the process during low-load times.

Visual Studio 2005 has a template, “Windows Service”, for CSharp and Visual Basic that can be used to simplify the creation of a new Service. After the creation of a new project using the template and applying an appropriate name, the necessary references and declarations are ready for use. The new project commences in the design view of the “Service1” class. Change the class name (“ScanWssService”, in the example) and go to the code view window:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace ScanWssWindowsService
   public partial class ScanWssService : ServiceBase
      public ScanWssService()

      protected override void OnStart(string[] args)
         // TODO: Add code here to start your service.

      protected override void OnStop()
         // TODO: Add code here to perform any tear-down
         // necessary to stop your service.

It also is possible to create an empty Visual Studio project and manually set the necessary references to System.dll and System.ServiceProcess.dll. The main executable class needs to inherit from System.ServiceProcess.ServiceBase. The class constructor contains the name of the service and can initialize other properties such as “CanStop”, “CanShutdown”, and “CanPauseAndContinue”. In the code generated by the Visual Studio template, the constructor calls the partial function, “InitializeComponent”; at this point, the “Name” property is configured by default and the other properties can be added as desired.

The template creates two override routines, for “OnStart” and “OnStop”, but other overrides may be created manually (“OnPause”, “OnContinue”, “OnShutdown”, “OnSessionChange”, and so on). Also, if it is necessary for the program’s logic, a “Main” method can be added to the class.

More by Author

Must Read