Geeks With Blogs

News




View Tarun Arora's profile on LinkedIn

profile for Tarun Arora at Stack Overflow, Q&A for professional and enthusiast programmers

Tarun Arora - Visual Studio ALM MVP ALM, Agile, Automation, Performance Testing, Software QA, Cloud, ...

 

Friends at ‘Twitter Sharp’ have created a wonderful .net API for twitter. With this blog post i will try to show you a basic TFS – Twitter integration scenario where i will retrieve the Team Project details programmatically and then publish these details on my twitter page. In future blogs i will be demonstrating how to create a windows service to capture the events raised by TFS and then publishing them in your social eco-system.

image  image

imageimage

1. Setting up Twitter API

  • Download Tweet Sharp from => https://github.com/danielcrenna/tweetsharp 
  • Before you can start playing around with this, you will need to register an application on twitter. This is because Twitter uses the OAuth authentication protocol and will not issue an Access token unless your application is registered with them.

image

 

    • Once you have registered your application, you will need ‘Customer Key’, ‘Customer Secret’, ‘Access Token’, ‘Access Token Secret’

image

2. Connecting to Twitter using the Tweet Sharp API

  • Create a new C# windows forms project and add reference to ‘Hammock.ClientProfile’, ‘Newtonsoft.Json’, ‘TweetSharp’

image

  • Add the following keys to the App.config (Note – The values for the keys below are in correct and if you try and connect using them then you will get an authorization failure error).

image

  • Add a new class ‘TwitterProxy’ and use the following code to connect to the TwitterService (Read more about OAuthentication - http://dev.twitter.com/pages/auth)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TweetSharp;
 
namespace WindowsFormsApplication2
{
    public class TwitterProxy
    {
        private static string _hero;
        private static string _consumerKey;
        private static string _consumerSecret;
        private static string _accessToken;
        private static string _accessTokenSecret;
 
        public static TwitterService ConnectToTwitter()
        {
            _consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
            _consumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
            _accessToken = ConfigurationManager.AppSettings["AccessToken"];
            _accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"];
 
            return new TwitterService(_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret);
        }
    }
}
  • Time to Tweet!
_twitterService = Proxy.TwitterProxy.ConnectToTwitter();
 
_twitterService.SendTweet("Hello World");
  • SendTweet will return the TweetStatus, If you do not get a 200 OK status that means you have failed authentication, please revisit the Access tokens.
--RESPONSE: https://api.twitter.com/1/statuses/update.json
HTTP/1.1 200 OK
X-Transaction: 1308476106-69292-41752
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.03040
X-Transaction-Mask: a6183ffa5f44ef11425211f25
Pragma: no-cache
X-Access-Level: read-write
X-Revision: DEV
X-MID: bd8aa0abeccb6efba38bc0a391a73fab98e983ea
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Content-Type: application/json; charset=utf-8
Date: Sun, 19 Jun 2011 09:35:06 GMT
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Last-Modified: Sun, 19 Jun 2011 09:35:06 GMT
Server: hi
Vary: Accept-Encoding
Content-Encoding: 
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked

 

3. Integrate with TFS

   1:  // Update the AppConfig with the URI of the Team Foundation Server you want to connect to, Make sure you have View Team Project Collection Details permissions on the server
   2:          private static string _myUri = ConfigurationManager.AppSettings["TfsUri"];
   3:          private static TwitterService _twitterService = null; 
   4:   
   5:          private void button1_Click(object sender, EventArgs e)
   6:          {
   7:              lblNotes.Text = string.Empty;
   8:   
   9:              try
  10:              {
  11:                  StringBuilder notes = new StringBuilder();
  12:   
  13:                  _twitterService = Proxy.TwitterProxy.ConnectToTwitter();
  14:   
  15:                  _twitterService.SendTweet("Hello World");
  16:   
  17:                  TfsConfigurationServer configurationServer =
  18:                  TfsConfigurationServerFactory.GetConfigurationServer(new Uri(_myUri));
  19:   
  20:                  CatalogNode catalogNode = configurationServer.CatalogNode;
  21:   
  22:                  ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(
  23:                      new Guid[] { CatalogResourceTypes.ProjectCollection },
  24:                      false, CatalogQueryOptions.None);
  25:   
  26:                  // tpc = Team Project Collection
  27:                  foreach (CatalogNode tpcNode in tpcNodes)
  28:                  {
  29:                      Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
  30:                      TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);
  31:   
  32:                      notes.AppendFormat("{0} Team Project Collection : {1}{0}", Environment.NewLine, tpc.Name);
  33:                      _twitterService.SendTweet(String.Format("http://Lunartech.codeplex.com - Connecting to Team Project Collection : {0} ", tpc.Name));
  34:   
  35:                      // Get catalog of tp = 'Team Projects' for the tpc = 'Team Project Collection'
  36:                      var tpNodes = tpcNode.QueryChildren(
  37:                          new Guid[] { CatalogResourceTypes.TeamProject },
  38:                          false, CatalogQueryOptions.None);
  39:   
  40:                      foreach (var p in tpNodes)
  41:                      {
  42:                          notes.AppendFormat("{0} Team Project : {1} - {2}{0}", Environment.NewLine, p.Resource.DisplayName, 
 
"This is an open source project hosted on codeplex"); 

43: _twitterService.SendTweet(String.Format(" Connected to Team Project: '{

0}' – '{1}' ", p.Resource.DisplayName, "This is an open source project hosted on codeplex"));

  44:                      }
  45:                  }
  46:                  notes.AppendFormat("{0} Updates posted on Twitter : {1} {0}", Environment.NewLine, @"http://twitter.com/lunartech1");
  47:                  lblNotes.Text = notes.ToString();
  48:              }
  49:              catch (Exception ex)
  50:              {
  51:                  lblError.Text = " Message : " + ex.Message + (ex.InnerException != null ? " Inner Exception : " + ex.InnerException : string.Empty); 
  52:              }
  53:          }

 

The extensions you can build integrating TFS and Twitter are incredible!

 

Share this post :
Digg This
Posted on Sunday, June 19, 2011 10:25 AM TFS2010 , TFS API | Back to top


Comments on this post: TFS 2010 SDK: Integrating Twitter with TFS Programmatically

# re: TFS 2010 SDK: Integrating Twitter with TFS Programmatically
Requesting Gravatar...
Hi Sumit,

Seems very interesting! Good stuff.

Cheers, Tarun
Left by Tarun Arora on Sep 28, 2011 10:58 AM

comments powered by Disqus

Copyright © Tarun Arora [Microsoft MVP] | Powered by: GeeksWithBlogs.net