Publish To Twitter

This building block enables you to publish fields from a component to Twitter when a page is published without incurring additional work for the end user.

Description

By adding the TBB to a component template and filling in the fields in the associated parameter schema you can specify fields from the associated component to publish to Twitter. 

You can set whether the Twitter publication occurs once, on every publish, when the component is created or the specified fields are changed, or only when the specified fields in the component change. 

Additionally you can set the TBB to create a link back to the published page.

Changes in this version include improved error handling, suppression of failure when posting duplicate twitter statuses and option to continue publishing if Twitter rejects the post. 

Creating the Twitter Application

Before starting to build the projects for Tridion, you need to create an application in Twitter. Log into Twitter and go to Settings / Connections and click the link to access settings for your applications in the Developer Section.

Click �Register a new application�, you should see the following screen

Twitter application registration

Fill the form in as follows:

Application Name
The name you type here will appear at the bottom of every tweet (via [Application Name])
Description
A brief description of the application � eg Twitter feed from our website
Application Website
Where people can download the application you have written � in this case it may be best to link back to the source code: here.
Organization
Your organization name
Website
A link to your website
Application type
Ensure you choose �Client�
Default access
This must be set to �Read & Write�

Then fill in the captcha and press save.

You will now be presented with a page showing you settings for your application. Make a note of the �Consumer Key� and the �Consumer Secret� � you will need this for your source code shortly.

Creating the Authorisation Page

You need a way to allow your application to post to your Twitter account. As Twitter has now switched to using OAuth for authenticating you must pre-authenticate to allow publishing to your account.

To do this, open the CreateTwitterSecret project (in the download) as a website. The page in the project has very little formatting so you can edit it and add your own should you wish to.

Open the source code in the AuthoriseTwitter.aspx page. Change the values of sConsumerKey and sConsumerSecret in lines 10 and 11 to your consumer key and consumer secret which you obtained from creating your Twitter Application. Build the site to ensure all compiles.

You need to add the output as a custom page under Tridion, so copy the AuthoriseTwitter.aspx and AuthoriseTwitter.aspx.vb files to your Tridion web directory (Normally similar to c:\program files\Tridion\Web) on your content management server. Add a custom page by right clicking on Custom Pages in the Tridion GUI, choose �New Custom Page�, give the page a title and set the URL to AuthoriseTwitter.aspx.

Creating the parameter schema

First you need to create the parameter schema within your publication. You can either do this by using the schema file in the solution (in the resources folder) or manually entering the schema as below.

Using the schema file

If you are going to use the file from the solution, create a new parameter schema. Go to the source tab and replace everything from [xsd:annotation] to [/xsd:element] with everything from [xsd:annotation] to [/xsd:element] in the schema file (TwitterParameters.xsd), then save the schema, note the schema URI and proceed to creating the assembly.

Manual procedure

If you have created the parameter schema using the file, ignore this section. Create a new parameter schema. Add the following fields to the schema:

  • StringFormat (text field, required)
  • PublishMode (text field, required)
  • PublicationTarget (text field)
  • LinkToPage (text field, required)
  • URLShortening (text field)
  • PublicationRootURL (text field)
  • TwitterUserToken (text field, required)
  • TwitterTokenSecret (text field, required)

Go back to the publish mode field and tick �Values will be selected from a list�. Add the following values:

  • Once
  • Always
  • When Created or Changed
  • When Changed

Set the field to be a drop down list and set whatever default value you would like.

Go back to the LinkToPage field, tick �Values will be selected from a list� and add the following values:

  • No
  • Yes

Set the field to be a drop down list and set whatever default value you would like.

Go back to the URLShortening field, tick �Values will be selected from a list� and add the following values:

  • is.gd
  • tinyurl.com

Set the field to be a drop down list and set whatever default value you would like.

Save the schema and note the URI.

TwitterParameters Schema

Creating the assembly

Open the solution and find the following line in the PublishToTwitter.vb file (on line 21, just above the class declaration)

<TcmTemplateParameterSchema("tcm:12-39658-8")> _

Replace the URI (�tcm:12-39658-8�) with the URI of the parameter schema you noted in the previous step.

On line 18, replace the URI (�tcm:12-8230-2�) with the URI of the folder you want the assembly to load into.

If you want to allow publishing to succeed if Twitter rejects the update, set the value of bPublishWhenTwitterFails (line 32) to True.  It is best to do this after successfully testing the building block to ensure it is properly working.

Change the values of sConsumerKey and sConsumerSecret on lines 30 and 31 to the consumer key and consumer secret of your application.

Add references to the following Tridion-supplied dlls: 

  • Tridion.Common.dll 
  • Tridion.ContentManager.dll 
  • Tridion.ContentManager.Publishing.dll 
  • Tridion.ContentManager.Templating.dll    

Build the solution and upload the assembly to your publication using the �TcmUploadAssembly.exe� utility as normal. 

Obtaining the keys for your Twitter Account

Before you can publish from Tridion you need to authorise your application with your Twitter account.

Click the AuthoriseTwitter custom page you created in Tridion.

Press �Login to Twitter� � a new window should open similar to the following.

Allow access to Twitter

Log in if required.

Press allow.

Take a note of the PIN number, close Twitter and type the PIN number into your custom page.

Press �Submit PIN�

Take a copy of the Token and Token Secret displayed in the custom page (these will be used in the Template Building Block parameters).

Usage

Add the Template Building Block (PublishToTwitter) to your compound component template. 

Media release default template

Click on the PublishToTwitter TBB and fill in the parameters as follows:

StringFormat 
Specify what you want to appear in Twitter. Use {} to denote a field from the component. 
Eg: Price drop � {Title} is now {price}! 
(where {Title} and {price} are fields from the component) 
or: Media release � {Title} 
(where {Title} is a field from the component) 

PublishMode 
Choose how frequently the Twitter feed should be updated by the publishing of the associated component. 

Once � Only publish to Twitter once (as there is no way to find out definitely whether the component has previously been published, this is as close as can be achieved - it looks through the components used on each version of the page and checks whether a page has been published since the component was put on it) 

Always � Publish to Twitter on every publish of the component 

When Created or Changed � Publish to Twitter as per �Once� and also when the fields used in �StringFormat� have changed since last publish 

When Changed - Publish to Twitter when the fields used in �StringFormat� have changed since last publish 

PublicationTarget 
If you wish to restrict the template to only update your Twitter status when publishing to a specific publication target, specify the URI of the publication target here. If you do not specify a publication target URI your Twitter status will be updated when published to any target (according to the PublishMode) 

LinkToPage 
Choose whether you want to add a link to your Twitter status (Tweet). This uses is.gd to shorten the URL of the publishing page and suffixes the link at the end of your status. If the length of your status with the link added is longer than the maximum Twitter supports, your status will be shortened to fit and ��� will be added before the url is suffixed. 

URLShortening
Choose whether you want to use is.gd or tinyurl.com for your short URLs.

PublicationRootURL 
If you have chosen to include a link to your page you need to specify the URL root to your website (eg http://www.atl.org.uk). This allows an absolute link to be created to the page from Twitter. 

TwitterUserToken 
Enter the Token you copied from your Twitter Authorisation custom page

TwitterTokenSecret
Enter the Token Secret you copied from your Twitter Authorisation custom page

Save and close the component template. 

When you next publish a page containing a component using this component template it will update Twitter according to your chosen publish mode. 

Your updates will then be available to anyone who follows you.

My Twitter Page


Developer
Company
:    ATL
License agreement
Download package
Tags
:    Web 2.0,  Social Networking,  Visual Basic
Version
:   1.3
Source
:   Yes

About the Author
Andrew Blackman
Developer

Solely responsible for development of the Association�s websites and related technologies.

Association of Teachers and Lecturers

Most Popular Building Blocks

These are the most popular Building Blocks. Did you already get them?