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;
}

}
}

3 comments:

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.
BATU March 11, 2020 at 11:21 PM

Thank you very much for providing important information
http://jewishrnb.com/
http://stagraydon.com/
http://ourdancingbear.com/
http://inglemanparrish.com/
http://mondogonfiabili.com/