如何在 WorkItemChangedEvent 中获取对 TFS WorkItem 的引用? [英] How can I get a reference to the TFS WorkItem in a WorkItemChangedEvent?

查看:20
本文介绍了如何在 WorkItemChangedEvent 中获取对 TFS WorkItem 的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这会超级简单,但我很难找到我需要的东西.

Seems like this would be super simple, but I'm struggling to find what I need.

我正在实现一个 TFS 2013 事件处理程序,只是想获得对引发更改事件的工作项的引用.获取标题似乎很容易,但我无法在事件签名对象中找到属性或方法来提供对 WorkItem 对象的引用或我需要查询它的信息(例如 ID).

I'm implementing a TFS 2013 event handler and simply want to get a reference to the Work Item that raised the change event. It seems easy enough to get the title, but I can't find a property or method in the event signature objects that gives me either a reference to the WorkItem object or the information I'd need to go query it (e.g. the ID).

public EventNotificationStatus ProcessEvent(
         TeamFoundationRequestContext requestContext, 
         NotificationType notificationType, 
         object notificationEventArgs, 
         out int statusCode, 
         out string statusMessage, 
         out ExceptionPropertyCollection properties)
    {          
        var ev = notificationEventArgs as WorkItemChangedEvent;
        string WorkItemTitle = ev.WorkItemTitle; /* easy enough */

        /*********** need help with this bit *********/
        int ChangedWorkItemID = ???
              OR
        WorkItem ChangedWorkItem= ???
    }

注意:此代码已被精简为基本结构,以便于阅读并专注于手头的问题.

Note: this code has been stripped down to bare bones to make it easier to read and focus on the problem at hand.

推荐答案

我找到了一种方法.它不像我想要的那么优雅,但它有效.如果有人有更好的答案,我绝对感兴趣.

I found a way to do this. It isn't as elegant as I'd like, but it works. I'm definitely interested if anyone has a better answer.

以下是对有相同问题的其他人有益的方法.

Here's what worked for the benefit of anyone else with the same question.

您可以从传递给事件处理程序的 notificationEventArgs(类型为 WorkItemChangedEvent)上的 CoreFields.IntegerFields 集合中获取工作项的 ID.使用它,您可以从 WorkItemStoreGetWorkItem 方法中获取 WorkItem.

You can get the Work Item's ID from the CoreFields.IntegerFields collection on the notificationEventArgs (of type WorkItemChangedEvent) passed into the event handler. Using that you can get the WorkItem from the WorkItemStore's GetWorkItem method.

注意:您想要的项目的字段名称为ID",并且它似乎始终是集合中的元素 0,但我不相信它总是如此,所以我使用LINQ 以防万一.这是整个事件的代码片段.

Note: The item you want has a field name of "ID", and it appears that it is always element 0 in the collection, but I didn't trust that always to be true so I searched on the name property using LINQ just in case. Here's a code snippet of the whole affair.

IntegerField idField =  ev.CoreFields.IntegerFields
                          .Where<IntegerField>(field => field.Name.Equals("ID"))
                          .FirstOrDefault<IntegerField>();

int WorkItemID=  idField.NewValue;

//Assuming you have an initialized WorkItemStore Object here
 workItemStore.GetWorkItem(WorkItemID);

这篇关于如何在 WorkItemChangedEvent 中获取对 TFS WorkItem 的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆