Click here to Skip to main content
15,897,187 members
Articles / Programming Languages / C#

TFS API - Process Template Currently Applied to the Team Project

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Dec 2011CPOL2 min read 11.2K   2  
How to use the TFS API to get the name of the Process Template that is currently applied to the Team Project
  • Download demo solution here

In this blog post, I’ll show you how to use the TFS API to get the name of the Process Template that is currently applied to the Team Project. You can also download the demo solution attached. I’ve tested this solution against TFS 2010 and TFS 2011.

image   image

1. Connecting to TFS Programmatically

I have a blog post that shows you from where to download the VS 2010 SP1 SDK and how to connect to TFS programmatically.

C#
private TfsTeamProjectCollection _tfs;
private string _selectedTeamProject;

TeamProjectPicker tfsPP = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
tfsPP.ShowDialog();
this._tfs = tfsPP.SelectedTeamProjectCollection;
this._selectedTeamProject = tfsPP.SelectedProjects[0].Name;

2. Programmatically Get the Process Template Details of the Selected Team Project

I’ll be making use of the VersionControlServer service to get the Team Project details and the ICommonStructureService to get the Project Properties.

C#
private ProjectProperty[] GetProcessTemplateDetailsForTheSelectedProject()
{
    var vcs = _tfs.GetService<VersionControlServer>(); 
    var ics = _tfs.GetService<ICommonStructureService>(); 
    ProjectProperty[] ProjectProperties = null;

    var p = vcs.GetTeamProject(_selectedTeamProject);
    string ProjectName = string.Empty;
    string ProjectState = String.Empty;
    int templateId = 0;
    ProjectProperties = null;
            
    ics.GetProjectProperties(p.ArtifactUri.AbsoluteUri, out ProjectName, 
      out ProjectState, out templateId, out ProjectProperties);

    return ProjectProperties;
}

3. What’s the Catch?

The ProjectProperties will contain a property “Process Template” which as a value has the name of the process template. So, you will be able to use the below line of code to get the name of the process template.

C#
var processTemplateName = processTemplateDetails.Where(
  pt => pt.Name == "Process Template").Select(pt => pt.Value).FirstOrDefault();

However, if the process template does not contain the property “Process Template”, then you will need to add it. So, the question becomes how do I add the Name property to the Process Template.

image

image

image

image

  • Download the Process Template from the Process Template Manager on your local machine
  • Once you have downloaded the Process Template to your local machine, navigate to the Classification folder within the template
  • From the classification folder, open Classification.xml
  • Add a new property <property name=”Process Template” value=”MSF for CMMI Process Improvement v5.0” />

4. Putting It All Together…

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Server;
using System.Diagnostics;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TfsAPIDemoProcessTemplate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private TfsTeamProjectCollection _tfs;
        private string _selectedTeamProject;

        private void btnConnect_Click(object sender, EventArgs e)
        {
            TeamProjectPicker tfsPP = new TeamProjectPicker(
                      TeamProjectPickerMode.SingleProject, false);
            tfsPP.ShowDialog();
            this._tfs = tfsPP.SelectedTeamProjectCollection;
            this._selectedTeamProject = tfsPP.SelectedProjects[0].Name;

            var processTemplateDetails = GetProcessTemplateDetailsForTheSelectedProject();

            listBox1.Items.Clear();
            listBox1.Items.Add(String.Format(
              "Team Project Selected => '{0}'", _selectedTeamProject));
            listBox1.Items.Add(Environment.NewLine);
            var processTemplateName = 
               processTemplateDetails.Where(pt => pt.Name == 
               "Process Template").Select(pt => pt.Value).FirstOrDefault();

            if (!string.IsNullOrEmpty(processTemplateName))
            {
                listBox1.Items.Add(Environment.NewLine);
                listBox1.Items.Add(String.Format(
                  "Process Template Name: {0}", processTemplateName));
            }
            else
            {
                listBox1.Items.Add(String.Format("The Process Template " + 
                  "does not have the 'Name' property set up"));
                listBox1.Items.Add(String.Format("***TIP: Download the Process Template " + 
                  "and in Classification.xml add a new property Name, update the " + 
                  "template then you will be able to see the Process Template Name***"));
                listBox1.Items.Add(String.Format(" - - - - - - - - - " + 
                  "- - - - - - - - - - - - - - - - - - - - - - -"));
            }
        }

        private ProjectProperty[] GetProcessTemplateDetailsForTheSelectedProject()
        {
            var vcs = _tfs.GetService<VersionControlServer>(); 
            var ics = _tfs.GetService<ICommonStructureService>(); 
            ProjectProperty[] ProjectProperties = null;

            var p = vcs.GetTeamProject(_selectedTeamProject);
            string ProjectName = string.Empty;
            string ProjectState = String.Empty;
            int templateId = 0;
            ProjectProperties = null;
            
            ics.GetProjectProperties(p.ArtifactUri.AbsoluteUri, out ProjectName, 
              out ProjectState, out templateId, out ProjectProperties);

            return ProjectProperties;
        }
    }
}

Thank you for taking the time out and reading this blog post. If you enjoyed the post, remember to subscribe to http://feeds.feedburner.com/TarunArora. Have you come across a better way of doing this. Please share your experience here. If you have questions/feedback/suggestions, etc., please leave a comment.

Thank you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Avanade
United Kingdom United Kingdom
Solution Developer - C# .NET, ALM

Tarun Arora is a Microsoft Certified professional developer for Enterprise Applications. He has over 5 years of experience developing 'Energy Trading & Risk Management' solutions using Microsoft Technologies. Tarun has great passion for technology and travel (not necessarily in the same order)!

Comments and Discussions

 
-- There are no messages in this forum --