Wednesday, July 28, 2010

Connecting to TFS2010 from a Webpart

With the implementation of Team Foundation Server 2010 the need for some integration with SharePoint 2010 was needed. Just to test the TFS API a simple webpart for registration of User Stories from SharePoint was created.

New to the TFS API I discovered that connection to the Project Collection needed a ICredentialsProvider (Fallback).  The UICredentialsProvider works fine in Windows Applications but when using the web, this provider can not be used…

The first step is to create a new Interface that uses the NetworkCredential class to return the credentials

public class NetworkCredentialsProvider : ICredentialsProvider
    {
        private readonly NetworkCredential credentials;
        public NetworkCredentialsProvider(NetworkCredential credentials)
        {
            this.credentials = credentials;
        }
        public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
        {
            return this.credentials;
        }
        public void NotifyCredentialsAuthenticated(Uri uri)
        {
        }
    }

Use this Provider to connect to the Project Collection

ICredentialsProvider TFSProxyCredentials;
var credentials = new NetworkCredential("Username", "Password", "Domain"); TFSProxyCredentials = new NetworkCredentialsProvider(credentials);

var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionUri, TFSProxyCredentials);

When running this code from a webpage/webpart the TFS will return an exception: “TF237121: Cannot complete the operation. An unexpected error occurred.”

This Exception is occurs because the TFS Client does not have a reference to a local Cache for storing files. (Normally this is stored in the current user profile area)

The solution is to let the TFS Client know where to store cache-files. This is done by defining a application setting named WorkItemTrackingCacheRoot and specifying a local path on the server (With proper NTFS rights)

The application setting can be created in web.config or programmatically

web.config:

<appSettings> <!-- Add reference to TFS Client Cache -->
   <
add key="WorkItemTrackingCacheRoot" value="C:\TFSClientCache"
/> </appSettings>

Code:

if (WebConfigurationManager.AppSettings["WorkItemTrackingCacheRoot"] == null || WebConfigurationManager.AppSettings["WorkItemTrackingCacheRoot"] == String.Empty)
{
   WebConfigurationManager.AppSettings["WorkItemTrackingCacheRoot"] = System.IO.Path.GetTempPath() + "TFSClientCache";
}

Thanks to Naren’s blog for pointing out the Client Cache settings

Click to download complete code to connect, and to get project info

 

2 comments:

Unknown said...

Thank you.... Got the same message when changing sharepoint configuration on TFS 2010 - the solution worked for us too.

Unknown said...

Thank you very much. You saved my day ;)