Ive been banging my head against this all week. Creating a new Team Project using the REST api. Everywhere i look, the response is the same, and it always involves using command line and xml.
But why?
On the visual studio online pages this can be found:
https://www.visualstudio.com/en-us/integrate/api/tfs/projects
(Specifically looking at the part labelled "Create a team project")
So why does this exist if it cant be used? Or am i missing something?
If anyone knows any examples of using this i would greatly appreciate it.
Ive been using the Microsoft.TeamFoundation.WorkItemTracking.Client namespaces etc... and have been happily creating new work items to projects
and Ive even managed to use the API to pull down lists of projects too. using code from this example (scroll to bottom of page)
https://www.visualstudio.com/en-us/integrate/get-started/rest/basics
but i cannot for the life of me post a new team project.
At this point i am open to any suggestions, i created an account here just to ask (i love this site) :(
As requested, some code:
static async Task<string> PostProjectAsync(HttpClient _client, string _apiUrl, string _apiVersion)
{
var responseBody = string.Empty;
HttpContent hc = new StringContent(@"
{
""name"": ""Testprojectfromconsole"",
""description"": ""Posted from console application using the tfs API""
}
");
//TODO: make a class that matches the json layout that the api is expecting
//then see if you have any better luck with that instead of this horrid horrid mess
ProjectPost newproj = new ProjectPost();
newproj.Name = @"Test Project -From console";
newproj.Description = @"Hopefully this has been posted from the console app, delete it later on if need be.";
newproj.Capabilities.VersionControl.SourceControlType = @"TFS"; //probably wrong
newproj.Capabilities.ProcessTemplate.TemplateTypeId = @"default"; //also probably wrong
string json = JsonConvert.SerializeObject(newproj);
try
{
using (HttpResponseMessage response = _client.PostAsync(_apiUrl + _apiVersion, hc).Result)
{
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return responseBody;
}
At the moment I'm passing a HttpContent called "hc" to the postasync, but if i switch it for the json object, the postasync stops working (because it wants httpcontent not a json)
before this methodis called, the client is set up as so:
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Set alternate credentials
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", ALTUSERNAME, ALTPASSWORD))));
Console.WriteLine("<--------------Getting projects from tfs!-------------->");
Console.WriteLine("<----------------Hold on to your butts!---------------->");
responseBody = await GetAsync(client, BASEURL + "projects", APIVERS);
Console.WriteLine(responseBody.ToString());
Console.WriteLine("<----------------Making a new project!----------------->");
Console.WriteLine("<----------------Hold on to your butts!---------------->");
responseBody = await PostProjectAsync(client, BASEURL + "projects", APIVERS);
Console.WriteLine(responseBody.ToString());
oh, and the URL is such:
static string PN1 = @"Test Project -From Web";
static string PN2 = @"Another Test Project -From Web";
static string COL = @"DefaultCollection";
static string BASEURL = "https://{0}.visualstudio.com/DefaultCollection/_apis/";
// Get the alternate credentials that you'll use to access the Visual Studio Online account.
static string ALTUSERNAME = "myusername";
static string ALTPASSWORD = "mypassword!";
//Your visual studio account name
static string ACCOUNT = "ourserver";
//Api version query parameter
static string APIVERS = "?api-version=1.0";