I'm trying to write an application that uses the Visual Studio Team Service (VSTS) API to display items from Visual Studio Online.
I have the following code that works perfectly in a Console application:
var connection = new VssConnection(new Uri(collectionUri), new VssClientCredentials());
using (var witClient = connection.GetClient<WorkItemTrackingHttpClient>())
{
var wiql = new Wiql {
Query = "SELECT [System.Id] FROM WorkItems WHERE State = 'New'"
};
var workItems = await witClient.QueryByWiqlAsync(wiql);
}
In a Console app, I receive a Visual Studio prompt to enter credentials, and everything works great. However, when I run the same code from WPF, I don't get the prompt, and the application just seems to hang.
I had this working in my WPF app for a brief moment after allowing it to run asynchronously from a button click event. Then I made some more changes, and--hours later--I just haven't been able to get it. I CTRL+Z'd my way back to the point where it was working and still--nothing. I suspect this has something to do with thread management, and I've tried everything I can think of with using Dispatcher.Invoke and running in Window_Loaded or hooking it up to a button.
I'm completely stumped.
I'm using the following NuGet package:
PM> Install-Package Microsoft.TeamFoundationServer.ExtendedClient
You need to start another thread to execute that (e.g. System.Threading.Tasks.Task.Run).
For example:
private void button_Click(object sender, RoutedEventArgs e)
{
var workitem = System.Threading.Tasks.Task.Run(() => GetItems(123)).Result;
}
public WorkItem GetItems(int itemId)
{
var myCredentials = new VssClientCredentials();
var vstsConnection = new VssConnection(new Uri(#"https://XXX.visualstudio.com/"), myCredentials);
var vstsClient = vstsConnection.GetClient<WorkItemTrackingHttpClient>();
var workItem = vstsClient.GetWorkItemAsync(itemId).Result;
return workItem;
}
On the other hand, you can specify account in your code directly (could be let user to provide account by using a custom login window).
var u = new Uri("XXX");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("[user name]", "[password]")));
var connection = new VssConnection(u, c);
Try with this:
TfsClientCredentials tcc = new TfsClientCredentials();
tcc.AllowInteractive = true;
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri("https://xjsdal.visualstudio.com"), tcc);
using (var witClient = ttpc.GetClient<WorkItemTrackingHttpClient>())
{
var wiql = new Wiql
{
Query = "SELECT [System.Id] FROM WorkItems WHERE State = 'New'"
};
var workItems = await witClient.QueryByWiqlAsync(wiql);
}
Related
I am trying to automate some processes to make life a bit easier. We have multiple requests from the team to create a folder in TFS 2017 (they do not have permissions) and then set up the associated builds for that source control folder.
The build creation part I think I have a way to do, but querying our on premise TFS 2017 server to get a list of folders under a certain path is proving tricky. So far I am having trouble even connecting to the server in the first place with this :
var collectionUri = "http://tfs-server:8080/tfs/DefaultCollection/";
var teamProjectName = "MYPROJECT";
Uri uri = new Uri(collectionUri);
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME", "PASSWORD", "COLLECTIONNAME")));
var connection = new VssConnection(uri, clientCredentials);
var sourceControlServer = connection.GetClient<TfvcHttpClient>();
That throws an exception : Error converting value "System.Security.Principal.WindowsIdentity;" to type 'Microsoft.VisualStudio.Services.Identity.IdentityDescriptor'
Can someone help me to get connected to the server first please! Documentation on this is very hard to find, and I dont see any examples that actually work.
What I was going to look at next was creating the folder if it doesn't exist. No idea how to do that yet, maybe using
sourceControlServer.GetBranchAsync(teamProjectName + FolderName);
Thanks!
EDIT:
Ok I got it to not error creating the connection by doing this instead :
Uri uri = new Uri("http://tfs-server:8080/tfs/DefaultCollection/");
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN")));
var buildServer = new BuildHttpClient(uri, clientCredentials);
var sourceControlServer = new TfvcHttpClient(uri, clientCredentials);
So now to just figure out how to list and create folders from TFS and to create builds!
EDIT:
So I have got the querying working, so I can check if a folder exists under a path like this :
var teamProjectName = "USA";
Uri uri = new Uri("http://tfs-server:8080/tfs/DefaultCollection/");
var clientCredentials = new VssCredentials(new WindowsCredential(new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN")));
TfvcHttpClient sourceControlServer = new TfvcHttpClient(uri, clientCredentials);
List<TfvcItem> branchItems;
using (sourceControlServer) {
branchItems = sourceControlServer.GetItemsAsync("$/USA/Development/NewFolder", VersionControlRecursionType.OneLevel).Result;
}
return branchItems.Count > 0;
That will find all the items under that folder. So if there isnt a folder, it will return 0, so I can go ahead and create that folder.
So next problem, is how to create the folder. Using CreateChangesetAsync.
Update:
To use Client API and CreateChangesetAsync method to create files in TFVC, you could refer below sample console app:
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
internal static async Task Main(string[] args)
{
var orgUrl = new Uri(args[0]);
string serverPath = args[1];
string localPath = args[2];
string contentType = args[3];
string pat = args[4];
var changes = new List<TfvcChange>()
{
new TfvcChange()
{
ChangeType = VersionControlChangeType.Add,
Item = new TfvcItem()
{
Path = serverPath,
ContentMetadata = new FileContentMetadata()
{
Encoding = Encoding.UTF8.WindowsCodePage,
ContentType = contentType,
}
},
NewContent = new ItemContent()
{
Content = Convert.ToBase64String(File.ReadAllBytes(localPath)),
ContentType = ItemContentType.Base64Encoded
}
}
};
var changeset = new TfvcChangeset()
{
Changes = changes,
Comment = $"Added {serverPath} from {localPath}"
};
var connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, pat));
var tfvcClient = connection.GetClient<TfvcHttpClient>();
await tfvcClient.CreateChangesetAsync(changeset);
}
}
}
Besides, instead of using tf command line, please kindly check solution here:
C# TFS API: show project structure with folders and files, including their ChangeType (checked out, deleted,renamed) like in visual studio
I'm creating an integration from our testing framework (Selenium) to Team Foundation Server (TFS) 2018 using C# (our tests are already written in it) - the integration will generate work items in TFS based on test results. I'd like to create the work item from a template, and I can't seem to find any documentation on doing so. I'm using the TFS client library from Microsoft found here.
I can pull a template from TFS:
var client = new HttpClient()
// Client auth stuff removed
var method = new HttpMethod("GET");
var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;
The API for creating new work items here looks fairly straightforward, but I can't find any way to connect the two actions, or a way to apply a template using this call. Is it possible to create a work item via API with a template, and if so, is there any documentation for it?
You can not create work item from work item template. You can use work item templates to see which fields contains some default values. This example for new work item from template with rest api:
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.Core.WebApi.Types;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static string ServiceURL = "https://your_name.visualstudio.com";
static string PAT = "your pat";
static void Main(string[] args)
{
string projectName = "your project";
string templateName = "Critical bug";
/*connect to service. I use VSTS. In your case:
VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
https://learn.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
*/
VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
//get clients
var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
var ProjectClient = connection.GetClient<ProjectHttpClient>();
var project = ProjectClient.GetProject(projectName).Result;
//get context for default project team
TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);
//get all template for team
var templates = WitClient.GetTemplatesAsync(tmcntx).Result;
//get tempate through its name
var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();
if (id != null)
{
var template = WitClient.GetTemplateAsync(tmcntx, id).Result;
JsonPatchDocument patchDocument = new JsonPatchDocument();
foreach (var key in template.Fields.Keys) //set default fields from template
patchDocument.Add(new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/" + key,
Value = template.Fields[key]
});
//add any additional fields
patchDocument.Add(new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = "My critical bug"
});
var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
}
}
}
}
My template:
I have a Visual Studio extension that we use internally for a project and one of the things it needs to be able to do is post tickets to VSTS. Previously we were using onsite TFS and making a connection to post tickets was as simple as:
var vssCreds = new VssCredentials(true);
projectCollection = new TfsTeamProjectCollection(url, vssCreds);
workItems = projectCollection.GetService<WorkItemStore>();
project = workItems.Projects["My Project"];
defaultType = project.WorkItemTypes["Bug"];
//...
var newItem = new WorkItem(defaultType)
{
Title = title
};
newItem.Fields["Assigned To"].Value = assignTo;
newItem.Fields["Repro Steps"].Value = repoSteps;
var validationResult = newItem.Validate();
newItem.Save();
And this worked fine. But after upgrading to VSTS I'm having a hard time getting the credentials part to work. I changed this line:
projectCollection = new TfsTeamProjectCollection(url, vssCreds);
To this:
projectCollection = new TfsTeamProjectCollection(url, new VssClientCredentials());
And this worked fine for me. But when I share it with other people on my team it didn't work at first and then started working a little later. I am guessing that interacting with VSTS caused their credentials to be loaded so that it then worked. But I have at least one person who just seems to be completely unable to make it work.
So what's the correct way to get it to use the VSTS credentials (that should already exist in VS)?
I see this overload for VssClientCredentials (https://msdn.microsoft.com/en-us/library/dn228355(v=vs.120).aspx):
public VssClientCredentials(
IVssCredentialPrompt credentialPrompt
)
Which I suspect might be useful, but I can't seem to find out if there's a built in implementation of IVssCredentialPrompt somewhere or, if not, how to implement it.
Remove the related key from
Computer\HKEY_CURRENT_USER\Software\Microsoft\VSCommon\14.0\ClientServices\TokenStorage\VisualStudio\VssApp, then authentication again.
You also can specify other Kind (vssApp by default) and Namespace (VisualStudio by default) by using this code:
var c = new VssClientCredentials();
c.Storage = new VssClientCredentialStorage(storageKind: "VssApp2", storageNamespace: "VisualStudio");
projectCollection = new TfsTeamProjectCollection(url, c);
For reasons that are entirely unclear and from this answer to an entirely different question: https://stackoverflow.com/a/40256731/1250301
It seems that spawning a new thread causes a login prompt to appear if needed and fix all the problems. So if I do this:
Task.Run(() =>
{
var url = new Uri(_tfsUrl);
var cred = new VssClientCredentials();
projectCollection = new TfsTeamProjectCollection(url, cred);
workItems = projectCollection.GetService<WorkItemStore>();
}).Wait();
project = workItems.Projects["Job Posting Data"];
defaultType = project.WorkItemTypes["Bug"];
taskType = project.WorkItemTypes["Task"];
Then it works. I have no idea why it works, or why this is necessary (at first I thought maybe it was a problem with not being in the UI thread so I'd tried Application.Current.Dispatcher.Invoke which did not work) but it seems to have fixed the problem.
I have two instances of the ChromiumWebBrowser in my WinForms project (Visual Studio 2012). My goal is to have the second browser instance "copy" the behavior of the user input in the first browser instance. I can successfully retrieve the input from the first browser, and I managed to hook up Selenium in the project as well.
However, I'm having one issue. Whenever Selenium sends its commands, the first browser is the one that responds to them. For the life of me, I can't seem to figure out how to make the second browser respond. Whenever I completely remove the first browser, the second one starts responding correctly, but adding the first browser again will make only have the first browser use the Selenium commands. I even tried to switch out the moments the browsers are added to the form, but to no avail: whenever there are two available, the wrong one is responsive.
Relevant code:
public BrowserManager(Controller controller, string startingUrl)
{
_controller = controller;
var settings = new CefSettings { RemoteDebuggingPort = 9515 };
Cef.Initialize(settings);
// Input browser
inputBrowser = new ChromiumWebBrowser(startingUrl);
var obj = new XPathHelper(this);
inputBrowser.RegisterJsObject("bound", obj); //Standard object registration
inputBrowser.FrameLoadEnd += obj.OnFrameLoadEnd;
// Output browser
var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };
var requestContext = new RequestContext(requestContextSettings);
outputBrowser = new ChromiumWebBrowser(startingUrl);
outputBrowser.RequestContext = requestContext;
outputBrowser.AddressChanged += InitializeOutputBrowser;
outputBrowser.Enabled = false;
outputBrowser.Name = "outputBrowser";
}
The selenium part:
public class SeleniumHelper
{
public SeleniumHelper()
{
DoWorkAsync();
}
private Task DoWorkAsync()
{
Task.Run(() =>
{
string chromeDriverDir = #"ActionRecorder\bin\x64\Debug\Drivers";
var chromeDriverService = ChromeDriverService.CreateDefaultService(chromeDriverDir);
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = #"ActionRecorder\bin\x64\Debug\ActionRecorder.exe";
options.DebuggerAddress = "127.0.0.1:9515";
options.AddArguments("--enable-logging");
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeDriverService, options))
{
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("A google search test");
query.Submit();
}
});
return null;
}
}
And finally, a screenshot for some visualization:
Some help with the issue would be very much appreciated. If i missed some crucial info, feel free to ask for it. Thanks in advance!
Greetz,
Tybs
The behavior is correct. You have one debug address and you can only have one debug address for CEF. Which means when you use Selenium it is only seeing one browser.
By default Selenium will send an command to current active Tab or Window. Now in your case you have multiple Chrome view embedded, but they are technically Chrome Tab/Windows which you have placed on the same form.
So if you are in luck below code in should be able to move you to the Window you are interested in
driver.SwitchTo().Window(driver.WindowHandles.Last());
See if it works. If it doesn't then your only other workaround would be to change the order of Adding ChromiumWebBrowser and that should reverse the window it works on.
Below are some important threads that you should read from top to bottom. Very relevant to your issue/request
https://code.google.com/archive/p/chromiumembedded/issues/421
https://github.com/cefsharp/CefSharp/issues/1076
I'm trying to create a new Website using the nuget package Microsoft.WindowsAzure.Management.WebSites (Version 3.0.0)
This tutorial has been helpful (even though its Java):
http://azure.microsoft.com/fi-fi/documentation/articles/java-create-azure-website-using-java-sdk/
except it suggests to use the WebSpaceNames.WestUSWebSpace constant.
var hostingPlanParams = new WebHostingPlanCreateParameters
{
Name = this.webhostingPlanName,
NumberOfWorkers = 1,
SKU = SkuOptions.Free,
WorkerSize = WorkerSizeOptions.Small
};
var result = new WebSiteManagementClient(this.Credentials)
.WebHostingPlans
.CreateAsync(WebSpaceNames.WestUSWebSpace, hostingPlanParams, CancellationToken.None)
.Result
This will result in an exception: NotFound: Cannot find WebSpace with name westuswebspace.
I actually want to create a custom WebSpace.
Except I can't find any method for it. See MSDN
So the only way I can make this work is using an existing WebSpace, that had created through the manage.windowsazure.com site. Which defeats the whole purpose of automating this.
The only Create[...] Method on IWebSpaceOperations is CreatePublishingUserAsync which I have tried running this as well but it results in an exception This operation is not supported for subscriptions that have co-admins. Which is pretty annoying in itself, doesn't make much sense to me, but is not really the core of my question.
I resolved this by using the prerelease package: PM> Install-Package Microsoft.Azure.Management.WebSites -Pre
Which works perfectly well. Except that it's only a pre-release of cause
// Create Web Hosting Plan
var hostingPlanParams = new WebHostingPlanCreateOrUpdateParameters
{
WebHostingPlan = new WebHostingPlan()
{
Name = "WebHostingPlanName",
Location = "Australia Southeast",
Properties = new WebHostingPlanProperties
{
NumberOfWorkers = 1,
Sku = SkuOptions.Standard,
WorkerSize = WorkerSizeOptions.Small
}
},
};
var result = this.ManagementContext.WebSiteManagementClient.WebHostingPlans.CreateOrUpdateAsync(
"ResourceGroupName",
"WebHostingPlanName",
CancellationToken.None).Result;
// Create Website
var websiteParams = new WebSiteCreateOrUpdateParameters
{
WebSite = new WebSiteBase
{
Location = "Australia Southeast",
Name = "WebSiteName",
Properties = new WebSiteBaseProperties
{
ServerFarm = "WebHostingPlanName"
}
}
};
var siteResult = this.ManagementContext.WebSiteManagementClient.WebSites.CreateOrUpdateAsync(
"ResourceGroupName",
"WebSiteName",
null,
websiteParams,
CancellationToken.None).Result;
If you want to use deployment slots you have to take this under consideration:
https://github.com/Azure/azure-sdk-for-net/issues/1088