TFS API-Process Template currently applied to the Team Project

Posted by Tarun Arora on Geeks with Blogs See other posts from Geeks with Blogs or by Tarun Arora
Published on Tue, 15 Nov 2011 23:29:00 GMT Indexed on 2011/11/16 1:52 UTC
Read the original article Hit count: 550

Filed under:

 

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.

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.

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.

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.

  • Download the Process Template from the Process Template Manager on your local

       image

  • Once you have downloaded the Process Template to your local machine, navigate to the Classification folder with in the template

      image

  • From the classification folder open Classification.xml

       image

  • Add a new property <property name=”Process Template” value=”MSF for CMMI Process Improvement v5.0” />

        image

 

4. Putting it all together…

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. Questions/Feedback/Suggestions, etc please leave a comment.

Thank You!

Share this post :

© Geeks with Blogs or respective owner