问题
I would like to develop software which runs every time a work item is being created or changed in a TFS team collection.
My TFS server version is 2013 and i'm planning to upgrade soon to 2015.
I tried the following article but it doesn't work on 2013...
What do you recommend?
回答1:
Found the answer!
here's my code:
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Server;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TFSClaritySyncEventHandler
{
public class WorkItemChangedEventHandler : ISubscriber
{
public string Name
{
get { return "WorkItemChangedEventHandler"; }
}
public SubscriberPriority Priority
{
get { return SubscriberPriority.Normal; }
}
public EventNotificationStatus ProcessEvent(
TeamFoundationRequestContext requestContext,
NotificationType notificationType,
object notificationEventArgs,
out int statusCode,
out string statusMessage,
out ExceptionPropertyCollection properties)
{
statusCode = 0;
properties = null;
statusMessage = String.Empty;
try
{
if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
{
var ev = notificationEventArgs as WorkItemChangedEvent;
StringBuilder sb = new StringBuilder();
if (ev.ChangeType == ChangeTypes.Change)
{
sb.AppendFormat("WorkItem {0} was changed!\n", ev.Title);
}
else
{
sb.AppendFormat("WorkItem {0} was created!\n", ev.Title);
}
if (ev.ChangedFields != null)
{
foreach (var item in ev.ChangedFields.IntegerFields)
{
sb.AppendFormat("{0} = {1} => {2}\n", item.Name, item.OldValue, item.NewValue);
}
foreach (var item in ev.ChangedFields.StringFields)
{
sb.AppendFormat("{0} = {1} => {2}\n", item.Name, item.OldValue, item.NewValue);
}
}
sb.Append("\nAll fields:\n***********\n");
foreach (var item in ev.CoreFields.IntegerFields)
{
sb.AppendFormat("{0} = {1}\n", item.Name, item.NewValue);
}
foreach (var item in ev.CoreFields.StringFields)
{
sb.AppendFormat("{0} = {1}\n", item.Name, item.NewValue);
}
EventLog.WriteEntry("Tfs Services", sb.ToString(), EventLogEntryType.Information);
}
}
catch (Exception exception)
{
TeamFoundationApplicationCore.LogException("Error processing event", exception);
}
return EventNotificationStatus.ActionPermitted;
}
public Type[] SubscribedTypes()
{
return new Type[] { typeof(WorkItemChangedEvent) };
}
}
}
result in event log:
work item creation log
WorkItem was created!
Area ID = -2147483648 => 2
Iteration ID = -2147483648 => 3
Priority = -2147483648 => 2
ID = -2147483648 => 28
Rev = -2147483648 => 1
PersonID = -2147483648 => 11
Watermark = -2147483648 => 54
Work Item Type = => Task
Area Path = => \TestTeamProject
Node Name = => TestTeamProject
Team Project = => TestTeamProject
Area Level 1 = => TestTeamProject
Iteration Path = => \TestTeamProject\Sprint 1
Iteration Level 1 = => TestTeamProject
Iteration Level 2 = => Sprint 1
Created By = |%00000000-0000-0000-0000-000000000000| => |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Changed By = |%00000000-0000-0000-0000-000000000000| => |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Changed Date = => 11/30/2015 7:29:41 PM
Title = => Task 1.2.2
State = => To Do
Reason = => New task
State Change Date = => 11/30/2015 7:29:41 PM
Created Date = => 11/30/2015 7:29:41 PM
Revised Date = => 1/1/9999 2:00:00 AM
Authorized Date = => 11/30/2015 7:29:41 PM
All fields:
***********
ID = 28
Rev = 1
Area ID = 2
Watermark = 54
Iteration ID = 3
Attached File Count = 0
Hyperlink Count = 0
External Link Count = 0
Related Link Count = 0
Work Item Type = Task
Title = Task 1.2.2
Area Path = \TestTeamProject
State = To Do
Reason = New task
Assigned To = |%00000000-0000-0000-0000-000000000000|
Changed By = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Changed Date = 11/30/2015 7:29:41 PM
Created By = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Created Date = 11/30/2015 7:29:41 PM
Authorized As = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Iteration Path = \TestTeamProject\Sprint 1
Authorized Date = 11/30/2015 7:29:41 PM
Revised Date = 1/1/9999 2:00:00 AM
work item changed log
WorkItem was changed!
Rev = 2 => 3
Watermark = 19 => 55
Changed Date = 8/12/2015 11:49:41 PM => 11/30/2015 7:35:46 PM
State = To Do => In Progress
Reason = New task => Work started
Remaining Work = => 10
Activity = => Development
State Change Date = 8/12/2015 11:48:54 PM => 11/30/2015 7:35:46 PM
Authorized Date = 8/12/2015 11:49:41 PM => 11/30/2015 7:35:46 PM
All fields:
***********
ID = 12
Rev = 3
Area ID = 2
Watermark = 55
Iteration ID = 4
Attached File Count = 0
Hyperlink Count = 0
External Link Count = 0
Related Link Count = 1
Work Item Type = Task
Title = Task 2.2.1
Area Path = \TestTeamProject
State = In Progress
Reason = Work started
Assigned To = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Changed By = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Changed Date = 11/30/2015 7:35:46 PM
Created By = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Created Date = 8/12/2015 11:48:54 PM
Authorized As = |guybe%b412f809-e7b9-4c99-93c4-810d4dc5bb8d|
Iteration Path = \TestTeamProject\Sprint 2
Authorized Date = 11/30/2015 7:35:46 PM
Revised Date = 1/1/9999 2:00:00 AM
来源:https://stackoverflow.com/questions/34002496/tfs-2013-event-handling-on-work-item-change