Write a WorkItem or Bug to TFS Preview in the Cloud

Ok, some folks just my search the interweb in how to connect to programmatically via C# to TFS Preview. Here is the how to guide.

So First you need the TFS SDK which you can find here.

Afterwards please make sure you have installed the KB2662296 Fix which is explained here.

That's all you need to get started.

So you start be Making a new Project. In my case I’d like to do a ASP.net MVC Application. So I just wanted to have Controller which is doing all the Magic.

public string SetWorkitems() { }

In order to connect to the TFS in the cloud you have to check out the Endpoint. Here is how this is supposed too look like:

  >> https://YOURACCOUNT.tfspreview.com/defaultcollection

Once you figured that out you just need to know how to authenticate against the TFS Preview. Here is the reason why you need to install the KB Fix. Same is true if you want to connect to TFS Preview in the Cloud via Visual Studio 2010 and Team Explorer. Since we use the Identity Service Provider. So that means somewhere behind that is a Resource STS checking if you are allowed to do what you are supposed to do.

The magical Class is ServiceIdentityCredentialsProvider which you should find in the Microsoft.TeamFoundation.Client.dll.

If it is missing you should Install the KB2662296 we discussed earlier.

Well you can now write your own Identity Service Factory to retrieve the Account or you can just use the Tool by Martin Hinshelwood.
Once you authenticated against your TFS Preview instance you get the corresponding User and Password strings.

public string SetWorkitems()
{
const string serviceUser = "Account Service (generated by Tool)";
const string servicePassword = "generated by Tool";
var collection = new Uri("https://YOURACCOUNT.tfspreview.com/defaultcollection");
const string teamProject = "YOURPROJECT";

    var provider = new ServiceIdentityCredentialsProvider(serviceUser, servicePassword);

    var tfs = new TfsTeamProjectCollection(collection, provider);
// check if authentication works
tfs.EnsureAuthenticated();

    var workItemStore = tfs.GetService<WorkItemStore>();
if (workItemStore==null)
{
//just for the error handling once more the call
var tmpTest = new WorkItemStore(tfs);
}

    if (workItemStore != null)
{
WorkItemTypeCollection workItemTypes = workItemStore.Projects[teamProject].WorkItemTypes;

        //Define Workitem Type as Bug
WorkItemType workItemType = workItemTypes["Bug"];

        //Assign values to each mandatory field
var workItem = new WorkItem(workItemType);

        string workItemTitle = "YOURWORKITEMTITLE";
string workItemIterationPath = @"YOURAREAPATH\Release 1";
string workItemAreaPath = "YOURAREAPATH";

workItem.Title = workItemTitle;
workItem.AreaPath = workItemAreaPath;
workItem.IterationPath = workItemIterationPath;

        //Check for Validation errors before saving
var validationErrors = workItem.Validate();
if (validationErrors.Count==0)
{
workItem.Save();
}
else
{
return validationErrors.Count.ToString(CultureInfo.InvariantCulture);
}
}

    return "success";
}

 

The rest is just API Stuff, which should be easy. Notice in order to Retrieve and work with Workitems there is a bunch of Team Foundation SDK References you will need to add. Here is the List.

image

That's it happy Extensioning!!!