Misha Sloushch
Misha Sloushch

Reputation: 21

How to Get Current Project Name from TeamFoundationRequestContext

I'm writing a plugin for a TFS process.

I need to get the Project Name from the TeamFoundationRequestContext whenever a work item is in the process of saving.

Normally I can get the work item ID because the record has already been saved. However, when the work Item is being saved the first time, I do not have a way to get the work item ID.

My question is how can I get the Project Name from the TeamFoundationRequestContext when the work item saves for the first time.

Upvotes: 0

Views: 319

Answers (2)

SoftwareCarpenter
SoftwareCarpenter

Reputation: 3923

Here is a class that captures the work item changed event and checks to see if it is a newly created work item.

From within the event if it is a newly created work item you can then do what you need with the Project name that is associated with that work item.

using Microsoft.TeamFoundation.Framework.Server;
using System;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Server;

namespace TfsProcess.CaptureProjectNameOnNewWorkItem
{
    public class CaptureProjectNameOnNewWorkItem : ISubscriber
    {


        public string Name
        {
            get
            {
                return "CaptureProjectNameOnNewWorkItem";
            }
        }

        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
            {
                ProcessNotification(notificationType, notificationEventArgs, requestContext);
            }
            catch (Exception exception)
            {
                TeamFoundationApplicationCore.LogException("Error processing event", exception);
            }
            return EventNotificationStatus.ActionPermitted;

        }

        private static void ProcessNotification(NotificationType notificationType, object notificationEventArgs, TeamFoundationRequestContext requestContext)
        {
            if (notificationType == NotificationType.Notification && notificationEventArgs is Microsoft.TeamFoundation.WorkItemTracking.Server.WorkItemChangedEvent)
            {
                var ev = notificationEventArgs as WorkItemChangedEvent;
                if (ev.ChangeType == ChangeTypes.New)
                {
                    //Do somethin with the project name of the newly created work item
                    // projectName = ev.PortfolioProject;

                }


            }
        }

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

Update:

You could create a plugin implementing the ITeamFoundationRequestFilter interface, which gets executed BEFORE or AFTER Team Foundation Server receives and processes requests. This allows you to validate work items and cancel the creation of the work item if it is not valid based on some logic for instance.

Here is a link to blog with a implementation of this that will cancel the creation of the work item if it is being created by a certain user.

Getting information from a TFS Request

How to implement ITeamFoundationRequestFilter

In order to get the Work Item ID you will intercept the xml soap response and parse it for the work item value.

Using the filter plugin you can filter for any of the methods responsible for updating, creating and querying work items.

The blog goes into more explanation and implementation.

Upvotes: 1

Daniel Mann
Daniel Mann

Reputation: 58980

The WorkItemChangedEvent event has a PortfolioProject property, so you should be able to get it like this:

var ev = (WorkItemChangedEvent)notificationEventArgs;
var projectName = ev.PortfolioProject

Upvotes: 0

Related Questions