Geeks With Blogs

News




View Tarun Arora's profile on LinkedIn

profile for Tarun Arora at Stack Overflow, Q&A for professional and enthusiast programmers

Tarun Arora - Visual Studio ALM MVP ALM, Agile, Automation, Performance Testing, Software QA, Cloud, ...

 

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 :
Posted on Tuesday, November 15, 2011 11:29 PM TFS2010 , TFS API , TFS2012 | Back to top


Comments on this post: TFS API-Process Template currently applied to the Team Project

# re: TFS API-Process Template currently applied to the Team Project
Requesting Gravatar...
When I do this I am subsiquently unable to open the process template in the Process Template Editor in Visual Studio 2010 - it dies with an error "a duplicate key '' exists'

Any ideas what that's all about and / or possible workarounds?
Left by Piers Williams on Jun 13, 2012 7:12 AM

# re: TFS API-Process Template currently applied to the Team Project
Requesting Gravatar...
I get the same error. Is this a bug in the process template editor perhaps? I am able to upload the template and create new projects with it and subsequently retrive the template name property from the project. It just will not load in the template editor.
Left by Mike on Oct 17, 2012 9:10 PM

comments powered by Disqus

Copyright © Tarun Arora [Microsoft MVP] | Powered by: GeeksWithBlogs.net | Join free