1. Windows Central Question's Avatar
    I am using TFS 2015. I'm having a problem in retrieving non XAML build information from my C# console App using the TFS API. Actually, for one project we have setup for XAML Build definations and for another project, it is non-XAML one. I am able to connect the TFS projects and able to get the XAML defination lists. However, while trying to fetch the Non-XAML definations or builds, it is always an empty array.My code is below,

    static void Main(string[] args)
    {
    try
    {
    var tfsUri = (args.Length < 1) ? new Uri(ConfigurationManager.AppSettings["ServerUri"]) : new Uri(args[0]);
    var userCreds = new NetworkCredential(
    ConfigurationManager.AppSettings["Tfs.User"], ConfigurationManager.AppSettings["Tfs.Password"], ConfigurationManager.AppSettings["Tfs.Domain"]);

    // var tfsServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri, userCreds);
    var tfsServer = new TfsConfigurationServer(tfsUri, userCreds);
    tfsServer.EnsureAuthenticated();

    //// Get the catalog of team project collections
    var collectionNodes = tfsServer.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);

    //// List the team project collections
    foreach (var collectionNode in collectionNodes)
    {
    //// Use the InstanceId property to get the team project collection
    var collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
    var teamProjectCollection = tfsServer.GetTeamProjectCollection(collectionId);

    //// Get a catalog of team projects for the collection
    var projectNodes = collectionNode.QueryChildren(new[] { CatalogResourceTypes.TeamProject }, false, CatalogQueryOptions.None);
    foreach (var projectNode in projectNodes)
    {
    /*
    Console.WriteLine("Collection: " + teamProjectCollection.Name); // Print the name of the team project collection
    Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName); // List the team projects in the collection
    Console.WriteLine(" Team Project Id: " + projectNode.Resource.Identifier);
    */

    //// Get a catalog of team builds for the collection
    var buildDefinitions = new BuildDefinition();

    // var buildDetailList = buildDefinitions.GetBuildDefinitionListFromProject(teamProjectCollection, projectNode.Resource.DisplayName);
    buildDefinitions.GetBuildDetailsFromProject(teamProjectCollection, projectNode.Resource.DisplayName, projectNode.Resource.Identifier);
    }
    }
    }
    catch (Exception ex)
    {
    ErrorLogger.LogError(ex);
    }
    }

    public class BuildDefinition
    {
    public void GetBuildDetailsFromProject(TfsTeamProjectCollection tfsProjectCollection, string projectName, Guid projectId)
    {
    var buildService = tfsProjectCollection.GetService<IBuildServer>(); // (IBuildServer)tfs.GetService(typeof(IBuildServer));

    var buildDefinitionsList = GetAllBuildDefinitionsFromTheTeamProject(buildService, projectName);
    foreach (var buildDefinition in buildDefinitionsList)
    {
    var bdentities = new BuildDefinitionEntities
    {
    ProjectId = projectId,
    ProjectName = buildDefinition.TeamProject,
    BuildTypeId = Convert.ToInt32(buildDefinition.Id),
    BuildTypeName = buildDefinition.Name,
    ProjectDetailPath = string.Format("{0} > {1}", buildDefinition.TeamProject, buildDefinition.Name)
    };

    var buildDetailSpec = buildService.CreateBuildDetailSpec(buildDefinition);
    buildDetailSpec.InformationTypes = null; // for speed improvement
    buildDetailSpec.MinFinishTime = DateTime.Now.AddDays(-21); // to get only builds of last 3 weeks
    buildDetailSpec.MaxBuildsPerDefinition = 1; // get only one build per build definintion
    buildDetailSpec.QueryDeletedOption = QueryDeletedOption.ExcludeDeleted; // get only active builds
    buildDetailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending; // get the latest build only
    buildDetailSpec.QueryOptions = QueryOptions.All;

    var buildDetailList = buildService.QueryBuilds(buildDetailSpec).Builds;

    //// List the team builds for the collection
    foreach (var buildDetail in buildDetailList)
    {
    bdentities.BuildId = buildDetail.RequestIds[0];
    bdentities.BuildName = buildDetail.BuildNumber;
    bdentities.ArtefactsPath = buildDetail.DropLocation ?? "No Artefacts";
    bdentities.BuildCompleted = Convert.ToDateTime(buildDetail.FinishTime) > Convert.ToDateTime(buildDetail.StartTime)
    ? Convert.ToDateTime(buildDetail.FinishTime)
    : Convert.ToDateTime(buildDetail.StartTime);
    bdentities.BuildStatus = buildDetail.Status.ToString();
    bdentities.SourceGetVersion = buildDetail.SourceGetVersion ?? string.Empty;

    if (!string.IsNullOrEmpty(buildDetail.Quality))
    {
    bdentities.BuildQuality = buildDetail.Quality;
    bdentities.BuildQualityChangedDate = Convert.ToDateTime(buildDetail.LastChangedOn);
    }

    BuildDefinitionDbOperations.ManageTfsBuildDefinitions(bdentities);


    }
    }
    }
    private static IBuildDefinition[] GetAllBuildDefinitionsFromTheTeamProject(IBuildServer buildServer, string projectName)
    {
    var buildDefinitionSpec = buildServer.CreateBuildDefinitionSpec(projectName);
    buildDefinitionSpec.TriggerType = DefinitionTriggerType.All;
    buildDefinitionSpec.Options = QueryOptions.Definitions;

    return buildServer.QueryBuildDefinitions(buildDefinitionSpec).Definitions;
    }
    }

    Actually, I am new to this Tfs system. Would you please guide me where I am wrong?
    11-13-2015 12:59 AM

Similar Threads

  1. Wake from sleep
    By Nutmegz in forum Microsoft Surface Pro 4
    Replies: 9
    Last Post: 11-15-2015, 02:38 PM
  2. How much talk time do I get on a 1gb plan?
    By Windows Central Question in forum Ask a Question
    Replies: 2
    Last Post: 11-13-2015, 02:18 PM
  3. My Lumia 720 Camera(Both) quality gets worse after each update, why?
    By Windows Central Question in forum Nokia Lumia 720
    Replies: 0
    Last Post: 11-12-2015, 11:59 PM
  4. Instagram Beta on my 930 - no posting to other sites
    By vEEP pEEP in forum Nokia Lumia 930
    Replies: 1
    Last Post: 11-12-2015, 10:48 PM
  5. Replies: 0
    Last Post: 11-12-2015, 10:48 PM
LINK TO POST COPIED TO CLIPBOARD