0

.NET Library for quick and easy Twitter integration

A few days ago I wanted to create a solution to automatically tweet new blog posts. Because I did not have enough time to write a custom component to communicate to twitter I started looking for a .NET Twitter library.

After some time I found a very simple and effective library named Twitterizer. With Twitterizer you can easily implement the OAuth security mechanism.

After adding the reference to the Twitterizer library I created a ItemEventReceiver that starts on the ItemAdded event. In that event I post the title and the url to twitter.

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);

    string message = string.Format(CultureInfo.InvariantCulture, "New post #msftplayground :{0} - {1}", ToTinyURLS(properties.Web.Site.Url, properties.ListItem), properties.ListItem.Title);

    //TODO Get OAuth from web
    OAuthTokens tokens = GetTwitterToken(properties);
    TwitterResponse<TwitterStatus> response = TwitterStatus.Update(tokens, message);
}

private OAuthTokens GetTwitterToken(SPItemEventProperties properties)
{
    OAuthTokens retVal = null;
    
    string consumerKeySecret = EncryptionUtility.DecryptedString(properties.Web.GetPropertyBagProperty(Constants.PropertyBagKeyConsumerKeySecret, string.Empty));
    string consumerKey = EncryptionUtility.DecryptedString(properties.Web.GetPropertyBagProperty(Constants.PropertyBagKeyConsumerKey, string.Empty));
    string accessToken = EncryptionUtility.DecryptedString(properties.Web.GetPropertyBagProperty(Constants.PropertyBagKeyAccessToken, string.Empty));
    string accessTokenSecret = EncryptionUtility.DecryptedString(properties.Web.GetPropertyBagProperty(Constants.PropertyBagKeyAccessTokenSecret, string.Empty));

    if (!string.IsNullOrEmpty(consumerKeySecret) | !string.IsNullOrEmpty(consumerKey) | !string.IsNullOrEmpty(accessToken) | !string.IsNullOrEmpty(accessTokenSecret))
    {
        retVal  = new OAuthTokens();
        retVal.AccessToken = accessToken;
        retVal.AccessTokenSecret = accessTokenSecret;
        retVal.ConsumerKey = consumerKey;
        retVal.ConsumerSecret = consumerKeySecret;
    }
    return retVal;

}

In the code above you see the registration of the event. Within the event I retrieve the OAuthTokens for the security to Twitter. The tokens I need for the authentication are saved within the property bag of the current web.

As you can see in the code it is quite easy to post a status update to twitter with Twitterizer. On the site you can find more examples:

http://www.twitterizer.net/

Related Posts

Azure Event Grid with Custom Events As of yesterday (16-8-2017) the public preview of Azure Event Grid is live. Azure Event Grid is a fully managed event routing service. Azure Event Gri...
ASP.NET Checkbox with confirmation message For one of my assignments I had to create a checkbox that displays a confirmation message when you uncheck a checkbox. I thought I would share my sol...
System.Web.UI.WebControls.WebParts.WebPart versus Microsoft.SharePoint.WebPartPa... When I started developing for SharePoint two years ago I was thrown in a big black hole and started to develop webparts by hands on experience. In th...
ASP.Net Server Controls In January of this year I wrote a post about creating a virtual earth web part (You can read about it here). I had done this by adding JavaScript to ...
ASP:Menu in IE8 Since a few days I have installed Internet Explorer 8 on my computer and I was curious how it would display certain sites. Till a few hours ago every...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.