Click here to Skip to main content
12,755,775 members (37,125 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 28 Jul 2014

TFS API Queue a New Build with Custom Parameters

, 28 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Queue a new build using different build definition attributes at runtime. This tip will show you how to queue a new Build with custom parameters using TFS API.


This tip shows how to queue new build request for DEV/SIT/UAT/PROD using the same build definition file without changing the actual build definition.


I faced a challenge when queuing a new build with custom parameters using TFS API. I found the solution. So the question will come up as to how do I deserialize this change and serialize it back into a definition, build request. When I created a build definition, I will set it to a particular build configuration like Dev or SIT or UAT or PROD. Let's assume I set to Dev. If I want to build it for UAT, I have to change the build definition manually. It is a very time consuming process to make a change every time because other properties like staging location, server drop folder are different for different build configurations.


I am here proving the solution, how to queue new build request by changing the build definition attributes at runtime as per the specific environments.

Using the Code

DLL references are required to connect to TFS programmatically.

Create a new Windows Forms Application project and add reference to TFS Common and client DLLs.

Note: If Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.Common do not appear on the .NET tab of the References dialog box, use the Browse tab to add the assemblies.

You can find them at %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0 OR %ProgramFiles%\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0.

using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Build;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Build.Common;
using System.Security.Principal;
using Microsoft.TeamFoundation.Build.Workflow;

namespace MyTestCustomBuild
    class Program
        static void Main(string[] args)
            string ChangesetNo = "23456";
            Uri collectionUri = new Uri("https://TFSUrl:8080/tfs");
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection
            (collectionUri, System.Net.CredentialCache.DefaultNetworkCredentials, new UICredentialsProvider());

            //get the currently logged in user
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            if (tfs.HasAuthenticated)
                WorkItemStore workItemStore = tfs.GetService<WorkItemStore>();
                Project teamProject = workItemStore.Projects["TFSProjName"];
                IBuildServer buildServer = tfs.GetService(typeof(IBuildServer)) as IBuildServer;

                IBuildDefinition buildDef = buildServer.GetBuildDefinition(teamProject.Name, "MyTestBuild");

                // Queue each build definition
                IBuildRequest req = buildDef.CreateBuildRequest();

                // Specify changeset no.
                req.GetOption = GetOption.Custom;
                req.CustomGetVersion = "C" + ChangesetNo.Trim();

                //Modify the staging location
                req.DropLocation = @"\\Server\StagingLocation\";

                //Modify process parameter(ConfigurationToBuild) 
                //at runtime without impacting the actual build definition process parameter
                req.ProcessParameters = UpdateBuildConfiguration(buildDef);

                //Modify process parameter (ServerDropFolder) at runtime 
                //without impacting the actual build definition process parameter
                req.ProcessParameters = UpdateServerDropFolder(buildDef);


It must be serializable using the XAML serialization engine if you want to add custom types as process parameters.

Below function will change the build configuration:

private static string UpdateBuildConfiguration(IBuildDefinition rqBuildDef)
        var process = WorkflowHelpers.DeserializeProcessParameters(rqBuildDef.ProcessParameters);

        BuildSettings buildSettings = new BuildSettings();
        buildSettings = (Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings)process["BuildSettings"];

        BuildSettings newbuildSettings = new BuildSettings();
        newbuildSettings = buildSettings;

        //Update configuration to build
        if (newbuildSettings.HasPlatformConfigurations)
            newbuildSettings.PlatformConfigurations[0].Configuration = "UAT";

        //Remove exiting build settings and add new ones
        process.Add("BuildSettings", newbuildSettings);

        rqBuildDef.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process);
        return rqBuildDef.ProcessParameters;

Below function will change the server drop folder:

private static string UpdateServerDropFolder(IBuildDefinition rqBuildDef)
        var process = WorkflowHelpers.DeserializeProcessParameters(rqBuildDef.ProcessParameters);
        string ProjectName = process["ProjectName"].ToString();

        process["ServerDropFolder"] = @"\\WebServer\Folder\Web\";

        rqBuildDef.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process);
        return rqBuildDef.ProcessParameters;


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


About the Author

Srinibas Das
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 28 Jul 2014
Article Copyright 2014 by Srinibas Das
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid