TFS2010: Create a new Team Project Collection from Powershell and C#

Grant Holliday’s blog

Senior Service Engineer, Microsoft Visual Studio Team Foundation Service

TFS2010: Create a new Team Project Collection from Powershell and C#

Rate This
  • Comments 4

Team Foundation Server 2010 has the great new Administration Console, however one of the shortcomings of it is that you have to run it on the TFS Application Tier itself. The team wants to have a tool that allows remote server administration, however it required more time than we had for this release. Now because I hate logging on to servers, I’ve started seeking out ways to do common tasks remotely.

Fortunately, the product was architected in a way that you can almost do everything in the admin console via web services and the TFS client & server APIs.

To Create a Team Project Collection, the normal way is to logon to the server, open the admin console and click through the UI.

Create TPC UI

Using Powershell

Fortunately, I found a script from Chris Sidi and all I had to do was make it compatible with the changes that we introduced after Beta2. All you have to do is start Windows PowerShell on your local workstation, replace the highlighted values and run the following script. You can also run this on the server itself, however you will need to start PowerShell using “Run as Administrator”.

# Load client OM assembly.
[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

$instanceBaseUrl = "http://tfsserver:8080/tfs/";
$tfsServer = new-object Microsoft.TeamFoundation.Client.TfsConfigurationServer $instanceBaseUrl;

$tpcSvc = $tfsServer.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService]);
$job = $tpcSvc.QueueCreateCollection(
    "MyCollection",      # collection name.
    "",                  # description.
    $false,              # don't make this the default collection.
    "~/MyCollection/",   # virtual directory.
    "Started",           # State after creation.
    $null,               # no tokens.
    "Server=SQLSERVER;Integrated Security=SSPI;",       # The SQL instance to create the collection on. Specify SERVER\INSTANCE if not using default instance
    $null,               # null because the collection database doesn't already exist.
    $null)               # null because the collection database doesn't already exist.

$collection = $tpcSvc.WaitForCollectionServicingToComplete($job)

 

Execution of the last line will block until the collection is completed. If the collection cannot be completed for any reason, you’ll receive an exception:

Exception calling "WaitForCollectionServicingToComplete" with "1" argument(s): "The collection servicing job did not succeed."

If this is the case, then you’ll have to open up the admin console and look through the “Logs” node to see why it failed.

 

C# code to do the same

Add references to: Microsoft.TeamFoundation.Client.dll and Microsoft.TeamFoundation.Common.dll

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;

namespace CreateTPC
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverUrl = "http://myserver:8080/tfs";
            string collectionName = "MyCollection";
            string sqlConnectionString = "Data Source=MYSQLSERVER;Integrated Security=True;";

            TfsConfigurationServer tfs = new TfsConfigurationServer(new Uri(serverUrl));
            ITeamProjectCollectionService tpcService = tfs.GetService<ITeamProjectCollectionService>();

            Dictionary<string, string> servicingTokens = new Dictionary<string, string>();
            servicingTokens.Add("SharePointAction", "None"); // don't configure sharepoint
            servicingTokens.Add("ReportingAction", "None"); // don't configure reporting services

            ServicingJobDetail tpcJob = tpcService.QueueCreateCollection(
                collectionName, // Collection name
                "", // description
                false, // IsDefaultCollection
                string.Format("~/{0}/", collectionName), // virtual directory
                TeamFoundationServiceHostStatus.Started, // initial state
                servicingTokens, // servicing tokens
                sqlConnectionString, // connection string
                null, // default connection string
                null  // database category connection strings
                );

            TeamProjectCollection tpc = tpcService.WaitForCollectionServicingToComplete(tpcJob);
        }
    }
}

 

  • It's great! thanks, I hope the document for PS in TFS 2010 will better soon.Now some cmdlet are hardly to use, best wishes!

  • How to create team project using the API

  • If I want to configure the SharePoint and Reporting Service, What should I do for the 'servicingTokens' ?

  • Check out Richard's post if you want to create a TPC with SharePoint and Reports

    msmvps.com/.../creating-a-tfs-team-project-collection-using-powershell.aspx

Page 1 of 1 (4 items)