How to: Initialize a WorkItem instance for the changed work item in WorkItemChangedEvent handler

Recently I’ve been asked about several questions that need to initialize a WorkItem instance in the web service handler for WorkItemChangedEvent. So I paste a sample code here to ease the future references.

One thing to note is that in order to query work items, the identity for the app pool that hosts the web service in IIS must has property permission granted. In TFS 2005 and 2008, work item permissions are based on area and iteration. See Team Foundation Server Permissions for more information.

using System;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace EventHandler
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WorkItemChangedEventHandler : System.Web.Services.WebService
{

[SoapDocumentMethod(
Action = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify",
RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
[WebMethod]
public void Notify(string eventXml)
{
try
{
string tfsUrl = "http://TFS_AT:8080";
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsUrl);

var workItemId = GetWorkItemID(eventXml);

var wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
var wi = wis.GetWorkItem(workItemId);


}
catch (Exception e)
{
var eventSource = "My WorkItemChangedEvent Handler";
var logName = "Application";
var machineName = "."; //local computer.

if (!EventLog.SourceExists(eventSource, machineName))
{
var eventObj = new EventSourceCreationData(eventSource, logName);
EventLog.CreateEventSource(eventObj);
}

var eventLog = new EventLog(logName, machineName, eventSource);
eventLog.WriteEntry(e.Message);
}
}

private int GetWorkItemID(string eventXml)
{
var doc = new XmlDocument();
doc.LoadXml(eventXml);
var path = "/WorkItemChangedEvent/CoreFields/IntegerFields/Field[ReferenceName='System.Id']/NewValue";
var node = doc.SelectSingleNode(path);

var id = int.Parse(node.InnerText);
return id;
}

}
}

5 comments:

guangming April 25, 2010 at 7:12 PM

Discount Handbags On Sale-Beautycasa Online : Official online destination for all things Louis Vuitton Handbags and purses including Luxury Handbags trends, designer reviews, handbag deals, and more.Hot sale Cheap cell phonesand cell phone china on our site! Fashion look and powerful functions for these mobile phones even discount cell phones! Wholesale HiPhone, 3G cell phones, PDA/Smart Phone now.China Cell PhonesDon't miss these cellphone!toy wholesale

wholesale clothing February 9, 2011 at 12:33 AM

I totally agree with you!Keep on posting!!! 2945abc45 0209

Stacey August 11, 2011 at 8:00 AM
This comment has been removed by the author.
Adobe Acrobat 9 Download June 16, 2013 at 7:33 AM

Acrobat 9 Pro Extended is the complete PDF solution for business and technical professionals.
Acrobat 9 Pro helps business and creative professionals communicate and collaborate more effectively and securely with virtually anyone, anywhere. Unify a wide range of content in a single organized PDF Portfolio. Collaborate through electronic document reviews. Create and manage dynamic forms. And help protect sensitive information.
Microsoft Project 2010 Download delivers innovative features across the entire family of products: Microsoft Project Standard 2010 Download and Microsoft Project Professional 2010 Download for occasional and professional project managers, and Project Professional 2010, combined with Microsoft Project Server 2010, for the power and depth to meet the needs of larger organisations.