I\'m using the .NET Client Libraries for VSTS/TFS 2015 to programmatically create a build definition based off of a template that I\'ve grabbed in another team project.
There isn't a way to pass in a template as a property of the build definition. However, there's another way to achieve it. You can clone/import/export build definition between team projects through .net libraries.
var cred = new VssCredentials(new WindowsCredential(new NetworkCredential(username, password)));
var buildClient = new BuildHttpClient(new Uri(collectionURL, UriKind.Absolute), cred);
var buildDef = (await buildClient.GetDefinitionAsync(sourceProj, buildDefId)) as BuildDefinition;
buildDef.Project = null;
buildDef.Name += "_clone";
await buildClient.CreateDefinitionAsync(buildDef, targetProj);
From above code you can authenticate to the team server and retreive the build definition object from the source project by the providing project name and the build definition id.
And then you need to remove the reference to the project. Since build definition contains a reference to the project it would not be possible to import it into a different project. Finally create a new build definition in target project providing the definition objecte retreived from previous project.
Next step is to export the build definition to a file so we can latter import it. By using a json serializer to serialize the build definition and save it to a file.
var buildDef = (await buildClient.GetDefinitionAsync(project, buildDefId)) as BuildDefinition;
buildDef.Project = null;
File.WriteAllText(filePath, JsonConvert.SerializeObject(buildDef));
Finally add a import method, more details please refer this link
if (!File.Exists(filePath))
throw new FileNotFoundException("File does not exist!", filePath);
Console.WriteLine($"Importing build definition from file '{filePath}' to '{project}' project.");
var buildDef = JsonConvert.DeserializeObject<BuildDefinition>(File.ReadAllText(filePath));
buildDef.Name = newBuildName;
await buildClient.CreateDefinitionAsync(buildDef, project);