Dibbus.com

Where design meets development

hdrTweetOnPublish

Tweet on publish

| 2 Comments

Last time I received a request from the client if it was possible to post to twitter within umbraco cms.

I took a look at the our.umbraco projectslist and found a great package, tweet on publish. It had all possibilities except it doesn’t work. Reason is the changed authentication mechanism of Twitter. The removed the basic authentication and replaced it by the OAUTH system.

So, I had to build it myself and found out that is was actuallu pretty simple. All you have to do is:

  1. Download Twitterizr, you can download it from here
  2. Request access tokens from Twitter
  3. Create url shortener
  4. Add event for Umbraco


Get stuff from Twitter

Because the application will always act on behalf of the same user (you) to request information it is possible to get an access token from Twitter. For this information login on http://dev.twitter.com/. Click ‘you Apps’ and then ‘Edit details’. Now there is an option at the right side of the screen: ‘application detail’. Click that link and then there is a new menu. Click on ‘my access tokens’ and Twitter will handle you tokens you can use within your application.

What do we have

For getting all things to work, we should have:

  1. ConsumerKey
  2. ConsumerSecret
  3. AccessToken
  4. AccesTokenSecret

All this information is from your Twitter account.

The code

Now, were ready to create an event for Umbraco:

Create a class which inherits umbraco.BusinessLogic.ApplicationBase

public UmbracoEvents()
 {
   Document.BeforeSave += new Document.SaveEventHandler(Document_BeforeSave);
 }

We need to capture the beforeSave event, I’ll come back to that later on.
Next we create the Handler:

private void Document_BeforeSave(Document sender, SaveEventArgs e)
{
	if ((sender.ContentType.Alias == "Artikel" && sender.getProperty("tweeted") != null)
	{
		if (sender.getProperty("tweeted").Value.ToString() == "1")
		{
			OAuthTokens oauthTokens = new OAuthTokens()
			{
				AccessToken = MyAccesToken,
				AccessTokenSecret = MyAccessTokenSecret,
				ConsumerKey = consumerKey,
				ConsumerSecret = consumerSecret
			};
			string title = sender.getProperty("titel").Value.ToString();
			string shortenedUrl = UrlShortner.MakeTinyUrl(umbraco.library.RequestServerVariables("HTTP_HOST") + umbraco.library.NiceUrlFullPath(sender.Id).Replace("/home", ""));
			string post = string.Format("{0} {1} - {2}", prefix, title, shortenedUrl);
			TwitterStatus.Update(oauthTokens, post);
			sender.getProperty("tweeted").Value = "0";
		}
	}
}

Options

As you can see it’s pretty simple, just because the requested accessTokens from Twitter.
When a document is published this event is launced, just before the document is published. This is important because a tweet should be optional. By default no tweet is posted. So, I’ve created a checkbox with alias ‘tweeted’ (just as in the package). When the checkbox is checked a tweet is composed using the node property ‘title’, the url and a prefix which could be somethin like “Read this!”.

A tweet format is looking like this: {prefix} {title} – {NiceUrl}. Take a look around for a nice URL shortener, I’ve used TinyUrl which is quit easy to implement.

After the tweet is send, the checked value will be reset and the document is published so there is no change for a new tweet when the document is published again. You can reverse this ofcourse to save the checked value en automate the tweet by default.

Hope thismakes sense, If there is some time left, I’ll create a new package for this.

Author: Folkert

I'm a webdeveloper, looking for the best experience, working between development and design. Just a creative programmer. When I'm getting tired of programming C#, i'd love to create 3D images in 3D Studio Max, play the guitar, create an app for Android or crush some plastics on a climbing wall or try to stay alive when i´m descending some nice white powdered snowy mountains on my snowboard.

2 Comments

  1. Hi there, I was also trying to use tweet on publish package for umbraco but unfortunately it does not works. I am a newbie in Umbraco (and .NET). Is it possible for you to create a package or let me know the step by step procedure to make this work?

    Thanks in advance

  2. Hi, Cool post thanks for the insite,

    If you did create a package this would be great for me also

Leave a Reply

Required fields are marked *.


Page optimized by WP Minify WordPress Plugin