Search code examples
pluginstfsworkitem

TFS 2012 - ISubscriber plugin. How to get name of project and possibly list of queries containing triggering WorkItem while plugin is being executed?


So, I have the following ISubscriber code:

public class Subscriber : ISubscriber
{
    public string Namek
    {
        get { return "Subscriber"; }
    }

    public SubscriberPriority Priority
    {
        get { return SubscriberPriority.Normal; }
    }

    public EventNotificationStatus ProcessEvent    (TeamFoundationRequestContext requestContext, NotificationType     notificationType, object notificationEventArgs, out int statusCode, out string     statusMessage, out     Microsoft.TeamFoundation.Common.ExceptionPropertyCollection properties)
    {
        statusCode = 0;
        properties = null;
        statusMessage = String.Empty;

        try
        {
            if(notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
            {
               var ev = notificationArgs as WorkItemChangedEvent;
               ...........
            }
        }
    }

    public Type[] SubscribedTypes()
    {
        return new Type[1] = { typeof(WorkItemChangedEvent) };
    }
}

The code works; meaning after the plugin is installed and TFS recognizes it, putting a breakpoint within the ProcessEvent method works.

My issue is that I'm trying to get the name of the project and the "Shared" queries belonging to the WorkItem whose change triggered this run to the Subscriber plugin, but I can't seem to find such references (nor do I know if they are provided via this interface, since info on the WorkItemChangedEvent is so sparse).

Any suggestions?


Solution

  • WorkItemChangedEvent ev = notificationEventArgs as WorkItemChangedEvent;
    

    You can use Tfs.Aggregator code as a reference for similar tasks reference

    You should follow this lines to solve your problem:

    1. Get the project work item is in
    2. Get "Shared Queries" of the project
    3. Get the "Work Item Ids" of each query results
    4. Check which one includes the currently changed "Work Item Id"

    So code should be like:

    var tfsCollection = new TfsTeamProjectCollection(new Uri(tfsUri));
    var tfsStore = tfsCollection.GetService<WorkItemStore>();
    var workItemId = ev.CoreFields.IntegerFields[0].NewValue;
    var eventWorkItem = tfsStore.GetWorkItem(workItemId);
    var project = eventWorkItem.Project;
    
    var queryHierarchy = project.QueryHierarchy;
    var queryFolder = queryHierarchy as QueryFolder;
    var queryItem = queryFolder["Shared Queries"];
    queryFolder = queryItem as QueryFolder;
    
    //below is the list including the queries which include work item this event is attached
    var queriesIncludingWorkItem = new List<QueryDefinition>();
    
    if (queryFolder != null)
    {
        foreach (QueryDefinition query in queryFolder)
        {
            var wiCollection = tfsStore.Query(query.QueryText);                            
    
            foreach (WorkItem workItem in wiCollection)
            {
                if (workItem.Id == eventWorkItem.Id)
                {
                    queriesIncludingWorkItem.Add(query);
                    break;
                }
            }
        }
    }
    

    By the way, afaik; you need to recycle the iis application pool after the dlls deployed to the "Plugins" folder in order to see the changes sometimes.