JG Vimalan's Blog

It is all about coding!

Connect to tfs via API and get the build status of team projects

The following code snippet  explains the way to connect to tfs programmatically and get the build status of team projects.

This will be helpful when you create a custom application to display the build status of certain team projects in tfs.

 using Microsoft.TeamFoundation.Client;
 using Microsoft.TeamFoundation.Build.Client;
 …
 …

 private IQueuedBuildsView[] myTfsQueuedBuilds= null;
 private IQueuedBuildsView myTfsBuildQueue;
         /// <summary>
        ///  Create initial connection to the TFS 2008 Build Server.
        /// </summary>
        public void Connect()
        {
            TeamFoundationServer tfs = new TeamFoundationServer(TfsUrl);
            IBuildServer buildServer = (IBuildServer)
            tfs.GetService(typeof (IBuildServer));
            ArrayList queuedBuildsList = new ArrayList();

            //teamProject is the string array of team project name in tfs
            foreach (string project in teamProject)
            {
                myTfsBuildQueue = buildServer.CreateQueuedBuildsView (project);               
               
                myTfsBuildQueue.StatusChanged += new
                       StatusChangedEventHandler(myTfsBuildQueue_StatusChanged);

               //1000 ms and ‘myForm’ is the instance of the page
                myTfsBuildQueue.Connect(1000, myForm);

                queuedBuildsList.Add(myTfsBuildQueue);
            }
           
            myTfsQueuedBuilds= new IQueuedBuildsView[queuedBuildsList.Count];
            for(int i=0; i<queuedBuildsList.Count; i++)
            {
                queuedBuilds[i] = (IQueuedBuildsView)queuedBuildsList[i];
            }
        }

        private void buildQueue_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            foreach(IQueuedBuildsView qbv in queuedBuilds)
            {
                if (e.Changed && qbv.QueuedBuilds.Length > 0)
                {
                    foreach (IQueuedBuild qb in qbv.QueuedBuilds)
                    {
                        //update status in UI
                    }
                }
            }           
        }

Advertisements

October 28, 2010 - Posted by | TFS

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: