We are moving to the web style builds on TFS 2015 (Update 4).
I've always been able to retrieve information about builds using the code below, but that is not retrieving our new builds created via the web interface.
Is there a reasonable way to modify my code to bring in both legacy builds and the new style builds?
If not, I assume it is time for me to figure out how to use the REST API. Any tips for an equivalent query would be appreciated.
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://SERVERINFO"));
IBuildServer buildServer = (IBuildServer) tfs.GetService(typeof (IBuildServer));
var buildDetail = buildServer.CreateBuildDetailSpec("*");
buildDetail.MinFinishTime = DateTime.Now.Date.AddDays(-1);
buildDetail.InformationTypes = null;
buildDetail.QueryDeletedOption = QueryDeletedOption.IncludeDeleted;
buildDetail.MaxBuildsPerDefinition = 1; //Only return the most recent of each build type...or comment out to return all builds with this definition
var builds = buildServer.QueryBuilds(buildDetail).Builds.Select(....
The old XAML build system uses a SOAP API.The task-based new vNet build system does not have a SOAP API. It's using REST API. I'm afraid you could not just modify the code to get new builds. They do not support Build vNext as they were written before their time.
Besides the SOAP API is slowly being replaced with a REST API, especially in some new features.Since you are moving to vNext build on TFS2015 update4. Highly recommend you stat to use Rest API.
You can access it from C# code either by querying the REST API directly or by using the Team Foundation Server Client NuGet package. A Sample:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.WebApi;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Uri tfsurl = new Uri("http://xxxx:8080/tfs/CollectionName");
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(tfsurl);
BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>();
List<Build> builds = bhc.GetBuildsAsync("ProjectName").Result;
foreach (Build bu in builds)
{
Console.WriteLine(bu.BuildNumber);
}
Console.ReadLine();
}
}
}
By using the Rest API in the libraries as above, you could be able to get both XAML and vNext builds.
Related
I have a service which import tasks from TFS and Azure Devops. I use Microsoft.TeamFoundationServer.Client 16.153.0
I'm trying to connect to TFS using next code
var httpClient = new WorkItemTrackingHttpClient(new Uri(_settings.ServerAddress), new VssBasicCredential(_settings.Login, _settings.Password));
var taskQuery = "..."
var queryResult = await httpClient.QueryByWiqlAsync(tasksQuery, timePrecision:true);
This code works only for first time. If I change login/password and import tasks again it still using previous login/password even if it wrongs. And It doesn't work for azure devops.
What do I wrong?
Try using the following code to connect to DevOps. It obtain the PAT you defined in the code:
Uri uri = new Uri(_uri);
string personalAccessToken = _personalAccessToken;
string project = _project;
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
I'm upgrading a C# app that talks to TFS/VSTS to use the latest TeamFoundation sdk.
I would like to connect and have the app prompt for credentials in the same way that Visual Studio does if you use that to connect to TFS.
I've downloaded the latest stable VSTS Api from nuget.org which is:
microsoft.teamfoundationserver.extendedclient.15.112.1.nupkg
I also reference assemblies it uses from my VS2017 install, here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer.
I've tried a number of combinations, but can't get it to prompt. My current code looks like this:
static void Main(string[] args)
{
try
{
var netCred = new NetworkCredential();
var basicCred = new VssBasicCredential(netCred);
var vssCred = new VssCredentials(basicCred);
vssCred.PromptType = CredentialPromptType.PromptIfNeeded;
var server = new TfsTeamProjectCollection(new Uri(serverName), vssCred);
server.Authenticate();
}
catch( Exception ex )
{
System.Console.WriteLine(ex.ToString());
}
System.Console.ReadKey();
}
It doesn't prompt, and instead outputs this exception:
Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException:
TF30063: You are not authorized to access
https://.visualstudio.com/. ---> System.Net.WebException: The
remote server returned an error: (401) Unauthorized. at
System.Net.HttpWebRequest.GetResponse() at
Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest
webRequest, WebException& webException) --- End of inner exception
stack trace --- at
Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest()
at
Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage
message, TimeSpan timeout) at
Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation
operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)
at
Microsoft.TeamFoundation.Framework.Client.LocationWebService.Connect(Int32
connectOptions, Int32 lastChangeId, Int32 features) at
Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Connect(ConnectOptions
connectOptions) at
Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Authenticate()
at Microsoft.TeamFoundation.Client.TfsConnection.Authenticate() at
VstsAuthTest.Program.Main(String[] args) in
S:\VstsAuthTest\Program.cs:line 26
How do I get it to prompt for and cache credentials?
The old version of the TeamFoundation sdk dlls I was using seemed to work ok. The reason I'm upgrading is because the C# app seems to refuse to connect to TFS when installed on a machine with only VS2017 and not VS2015. I was hopeful upgrading to the latest SDK dlls might help solve the connection issue.
I've seen this, but it seems out of date and uses classes that are now deprecated. It's also about connecting without a prompt, but the comments include some discussion of how to get a prompt.
https://blogs.msdn.microsoft.com/buckh/2013/01/07/how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials/
I've also seen these samples which appear recent, but which also use deprecated apis.
https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/samples
Just use Microsoft Team Foundation Server Extended Client package with VssClientCredentials.
Simple code:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TFSAPIConsoleApp
{
class Program
{
static void Main(string[] args)
{
var u = new Uri("https://XXX.visualstudio.com");
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(u, new VssClientCredentials());
collection.Authenticate();
Console.WriteLine(collection.Name);
Console.Read();
}
}
}
I am trying to fetch Build Warning from MS Build,(in Build which contain or having number of solutions)
Is it possible to fetch using TFS API, or any TFS DB using QUERY ?
You could use this TFS REST API to get logs of a TFS builds. To get those logs out, you need to fetch those warnings by yourself. There's no API to only get warnings.
Http method: GET
http:/servername"8080/tfs/DefaultCollection/teamproject/_apis/build/builds/391/logs?api-version=2.0
You could also install a TFS ExtendedClient Nuget package to use TFS object model API.
Here is the code snippet:
Like the comment said above, the VNext build definition information couldn't be reached using the old version API. Install this TFS ExtendedClient Nuget package for your project, using the method below to get all build definitions.
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Operations;
private static void GetBuildWarnings()
{
var u = new Uri("http://v-tinmo-12r2:8080/tfs/MyCollection/");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("username", "password", "domain")));
var connection = new VssConnection(u, c);
BuildHttpClient buildServer = connection.GetClient<BuildHttpClient>();
List<BuildLog> logs = buildServer.GetBuildLogsAsync("teamprojectname",buildId).Result;
foreach (BuildLog log in logs)
{
var list = buildServer.GetBuildLogLinesAsync("A92FB795-A956-45B5-A017-7A7DFB96A040",buildId,log.Id).Result; //A92FB795-A956-45B5-A017-7A7DFB96A040 is the team project Guid
foreach (var line in list)
{
if (l.Contains("[Warning]"))
{
Console.WriteLine(line);
}
}
}
Console.ReadLine();
}
I want to add or remove users from TFS using REST API. Any help appreciated.
Afraid this can't be achieved through Rest API for now. If you really want to do it programmatically. You can use client API.
You can try to use IIdentityManagementService.ReadIdentity() along with IIdentityManagementService.AddMemberToApplicationGroup() to add Windows users to TFS groups, even if those Windows users are not known to TFS yet.
This is accomplished by specifying the ReadIdentityOptions.IncludeReadFromSource option.
Below is an example of adding a Windows user VSALM\Barry to the Fabrikam Fiber Web Team (TFS Group), in the FabrikamFiber Team Project, in the http://vsalm:8080/tfs/FabrikamFiberCollection (Also applies to server level)
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://vsalm:8080/tfs/FabrikamFiberCollection"));
var ims = tpc.GetService<IIdentityManagementService>();
var tfsGroupIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
"[FabrikamFiber]\\Fabrikam Fiber Web Team",
MembershipQuery.None,
ReadIdentityOptions.IncludeReadFromSource);
var userIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
"VSALM\\Barry",
MembershipQuery.None,
ReadIdentityOptions.IncludeReadFromSource);
ims.AddMemberToApplicationGroup(tfsGroupIdentity.Descriptor, userIdentity.Descriptor);
}
}
}
I need to know how many times a Work Item had the state Resolved.
Is it possible using the Work > Queries or some Widget in Home > Overview tabs in TFS 2015 or VSO/VSTS?
Is it possible using TFS API?
It cannot be achieved by Query, but you can get the information via TFS .NET Client Library API or Rest API.
For .NET Client Library API, you need to get the workitem first and then check every revision in the workitem to count the revisions with "Resolved State".
For Rest API, you can get all the revisions of the workitem by following this page: Get a list of work items revisions and then check how many times the workitem state was "Resolved".
Add a simple code sample for .NET Client Library API:
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace CountState
{
class Program
{
static void Main(string[] args)
{
string url = "https://xxxxxx.visualstudio.com";
string state = "Resolved";
int workitemid = 111;
int statecount = 0;
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
WorkItemStore wis = ttpc.GetService<WorkItemStore>();
WorkItem wi = wis.GetWorkItem(workitemid);
foreach (Revision rev in wi.Revisions)
{
if (rev.Fields["State"].Value.ToString() == state)
{
statecount = statecount + 1;
}
}
}
}
}