This blog is moved to
http://amalhashim.wordpress.com

Wednesday, February 24, 2010

Creating a Timer job in MOSS

First we need to have a class derived from SPJobDefinition

Below is the skeleton of that class.
namespace TimerNameSpace
{
#region Using
using System;
using System.Collections.Generic;  
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;  
using Microsoft.SharePoint.Utilities;
#endregion Using

/// <summary>
/// Timer service
/// </summary>
public class MyTimerService : SPJobDefinition
{
#region Constructor

/// <summary>
/// Initializes a new instance of the TimerService class
/// </summary>
public MyTimerService()
: base()
{
}

/// <summary>
/// Initializes a new instance of the MyTimerService class
/// </summary>
/// <param name="jobName">Job Title for the timer service</param>
/// <param name="webApp">Web appication for which this service will run</param>
public MyTimerService(string jobName, SPWebApplication webApp)
: base(jobName, webApp, null, SPJobLockType.ContentDatabase)
{
this.Title = "My Timer";
}
#endregion Constructor

#region Execute

/// <summary>
/// Overriden Execute method
/// </summary>
/// <param name="targetInstanceId">guid of the target object</param>
public override void Execute(Guid targetInstanceId)
{
try
{
/// Here the timer logic goes
}
catch (Exception ex)
{
//Log exception
}
}

#endregion Execute      
}
}
One this class is ready. Next thing we need to look into is creating a Feature. This feature will internally schedule the timer as a Job and runs it. Below is the snapshot of this class.

namespace TimerNameSpace
{
#region Namespace Inclusions
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;  
#endregion Namespace Inclusions

/// <summary>
/// Class is feature receiver for feature to install approval timer service
/// </summary>
public class MyTimerFeature : SPFeatureReceiver
{
/// <summary>
/// Notifications job name
/// </summary>
public string MyTimerFeatureName = "MyTimerServiceFeature";

/// <summary>
/// Occurs after a Feature is installed.
/// </summary>
/// <param name="properties">An  object that represents the properties of the event.</param>
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

/// <summary>
/// Occurs when a Feature is uninstalled.
/// </summary>
/// <param name="properties">An object that represents the properties of the event.</param>
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}

/// <summary>
/// Occurs after a Feature is activated.
/// </summary>
/// <param name="properties">An object that represents the properties of the event.</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// register the the current web
SPWeb web = (SPWeb)properties.Feature.Parent;

SPSite site = web.Site;

// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == this.MyTimerFeatureName)
{
job.Delete();
break;
}
}

MyTimerService myJob = new MyTimerService(this.MyTimerFeatureName, site.WebApplication);

SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 5;
schedule.Interval = 2;

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

/// <summary>
/// Occurs when a Feature is deactivated.
/// </summary>
/// <param name="properties">An object that represents the properties of the event.</param>
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// current web
SPWeb web = (SPWeb)properties.Feature.Parent;

SPSite site = web.Site;

// delete the job
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == this.MyTimerFeatureName)
{
job.Delete();
break;
}
}
}
}
}
Similar to SPMinuteSchedule, there is Hourl Schedule also. Use the one as per your requirement.
Once this much is done, we need to create Feature.xml file for the feature we have built and paste it under the Features Folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES). Create a folder named MyTimerFeature and under this folder create Feature.xml with the following content.


<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Creator="Amal Hashim"
Id="{76D2F200-5CA0-4882-B64F-9FC6208C1234}"
Title="MyTimerFeature"
Description="My Timer."
Scope="Web"
Hidden="FALSE"
Version="1.0.0.0"
ReceiverAssembly="MyTimer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12344ab009fa4028"
ReceiverClass="TimerNameSpace.MyTimerFeature">
</Feature>
You can activate the feature using the Stsadm command as shown below.
stsadm -o installfeature -name MyTimerFeature

For checking whether the timer has activated successfully or not, you can go to the central admin page
Central Administration –> Operations –> Timer Job Status

Listing all modified objects in SQL Server by Date

 

select * from sys.objects where modify_date >= '2010-02-16'

This will list all the database objects modified after 16th Feb 2010

Wednesday, February 10, 2010

New Security Patches

Hi all, Microsoft has released new set of security patches. These all are marked as critical, so install it as early as possible

Critical (2000, XP, W7, 2003, 2008 R2)/Important (Vista, 2008): This is another in the recent problems for Windows’ SMB handling; this one is a remote code execution exploit. The only nice thing about this one is that it requires the attacker to get you to try to connect to their rigged SMB server, and that’s pretty unlikely to go through many corporate firewalls. All the same, get this patch installed as soon as you can. 191KB - 1.2MB
http://www.microsoft.com/technet/security/bulletin/ms10-006.mspx
http://support.microsoft.com/kb/978706

Critical (2000, XP, 2003): There is a bug in the ShellExecute API call (which allows programs to ask the OS to perform commands) which allows a remote code execution attack to occur. This patch should be installed immediately. 606KB - 1.4MB
http://www.microsoft.com/technet/security/bulletin/ms10-007.mspx
http://support.microsoft.com/kb/975713

Critical (2000, XP)/Important(Vista, W7)/Moderate(2003)/Low(2008, 2008 R2): This is an important update to the ActiveX Kill Bits system that fixes a bug that could allow remote code execution exploits, and adds some addition controls to the kill bits system. Install this as soon as you can. 27KB - 1.0MB
http://www.microsoft.com/technet/security/bulletin/ms10-008.mspx
http://support.microsoft.com/kb/978262

Critical (Vista, 2008): A problem in the TCP/IP stack of Vista and 2008 allows and attacker to perform a remote code execution exploit if IPv6 is turned on. You should install this patch immediate. 1.4MB - 2.7MB
http://www.microsoft.com/technet/security/bulletin/ms10-009.mspx
http://support.microsoft.com/kb/974145