Clearly, as all the new features in this latest version of the product, the system timer job can be extended through the creation of their job.
What to do? Nothing could be easier, just create the usual class library. NET, marked by a key and add a new class that inherits from the class SPJobDefinition (in this namespace Microsoft.SharePoint.Administration). This class requires the definition of three builders, and a method, the Execute method, which will contain the code. NET that we want to execute the predetermined intervals.
In the example I decided to offer, I created a timer job to control when a workflow (specifically un'approval workflow) has come to an end. This was a real need, because there is no event that allows us to determine when the workflow is completed or not (even ItemUpdated the event of a list, because it is not triggered).
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace Peppe.Sharepoint.Jobs
{
public class WorkflowMonitor: SPJobDefinition
{
public WorkflowMonitor (): base () {}
public WorkflowMonitor (string jobName, SPService service,
SPServer server, targetType SPJobLockType)
: Base (jobName, service, server, targetType) {}
public WorkflowMonitor (string jobName, SPWebApplication WebApplication)
: Base (jobName, WebApplication, null, SPJobLockType.ContentDatabase)
{
this. Title = "Approval Workflow Monitor";
}
public override void Execute (Guid targetInstanceId)
{
SPSite site = new SPSite ("http://localhost");
using (site)
{
SPWeb web = site.OpenWeb ();
using (web)
{
Web.Lists SPList list = ["listname"];
SPListItemCollection items = list.Items;
foreach (SPListItem item in items)
{
if (item.Workflows [0]. IsCompleted)
{
/ / WORKFLOW COMPLETED!
}
if (item ["FieldName"]! = null)
{
if (item ["FieldName"]. ToString () == "16")
{
/ / ITEM APPROVED
}
if (item ["FieldName"]. ToString () == "17")
{
/ / ITEM REFUSED
}
}
}
}
}
}
}
}
Bookmarks