Clone Build Definition using TFS 2010 API

When you branch from Main or Dev branch you will move all source files and artifact to the new branch since build architecture will be the same. You have the option to create or clone the build definition. Personally i prefer to clone the parent build definition instead of creating new one because may you have a specific configuration for that build and you don’t want to lose it or create it each time.
For more information about branching architecture you can refer to this link

Below is the code how to clone the build definition using TFS API

        private void CloneBuildDefinition(string TeamSystem, string ParentDefinitionName, string DefinitionName, string ServerItem)

            IBuildServer buildServer = (IBuildServer)server.GetService(typeof(IBuildServer));

            //clone the parent branch build definition
            IBuildDefinition _buildDefinition = buildServer.GetBuildDefinition(TeamSystem, ParentDefinitionName);
            IBuildDefinition buildDefinitionClone = CloneBuildDefinition(_buildDefinition, DefinitionName);

            //get the new build definition properties to change the Maping and process params to new branch
            //instead of parent cloned branch.
            _buildDefinition = buildServer.GetBuildDefinition(TeamSystem, DefinitionName);

            //bind the new build settings from new build definition to replace the values.
            Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings _buildSettings = new Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings();
            StringList _parentSolutionlist = new StringList();
            StringList _currentSolutionlist = new StringList();

            //Deserialize Process Parameters to extract the BuildSettings.
            var process = WorkflowHelpers.DeserializeProcessParameters(_buildDefinition.ProcessParameters);
            _buildSettings = (Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings)process["BuildSettings"];
            _parentSolutionlist = _buildSettings.ProjectsToBuild;

            //iterate over all of solution to build and change the branch name.
            foreach (string list in _parentSolutionlist)
                _currentSolutionlist.Add(list.Replace("PARENTBRANCHNAME", "CURRENTBRANCHNAME"));
            //remove exiting build settings.
            _buildSettings.ProjectsToBuild = _currentSolutionlist;

            //assign new build settings.
            process.Add("BuildSettings", _buildSettings);
            _buildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process);

            //change the workspace mapping to point to new branch
            //remove the parent server mapping item.
            List<IWorkspaceMapping> _map = _buildDefinition.Workspace.Mappings;
            foreach (IWorkspaceMapping mapItem in _map)
                if (mapItem.ServerItem.Contains("PARENTBRANCHNAME"))
                    //add new branch server mapping item.
                    _buildDefinition.Workspace.AddMapping(ServerItem, string.Format("$(SourceDir)\\{0}", TeamSystem), WorkspaceMappingType.Map);




4 thoughts on “Clone Build Definition using TFS 2010 API

  1. Forgive me for correcting you, however, it seems this code cannot be called from anywhere, as you say in your comment from July.

    Line 6 reads:
    IBuildDefinition buildDefinitionClone = CloneBuildDefinition(_buildDefinition, DefinitionName);

    This other method CloneBuildDefinition (an overload perhaps?) takes only 2 parameters and returns an IBuildDefinition. This method is not defined in your sample.

    Would you please share with the rest of us the complet code sample?

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *