Publishing Sitefinity Content to Twitter with Tweetinvi

Publishing Sitefinity Content to Twitter with Tweetinvi

Posted on November 24, 2014 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

This post exposes a way to implement selective and instant Twitter publishing for items upon their creation. The approach relies on Sitefinity's EventHub  data provider events, and a third party API - Tweetinvi.

First off a big thank you goes to the guys from Tweetinvi for providing a C# API we can take advantage of. In order to have access to their API you will need to download their assemblies and add them to your application (see link above).

Once done you'll have access to their methods, and you are good to go. Next up is to get Sitefinity to play nice with them. The scenario I am going to cover is to publish a dynamic content type (Sample Content) item and decide whether to tweet the content of the item based on the value of a Yes/No field. 

In order to take advantage of Tweetinvi's methods you will first need to authenticate a Twitter application. Once done you are authenticated on a thread scope, so there is no need to do so with every tweet. You can use the Default Sitefinity Twitter application. If you chose to use your own application, please add it in the backend and get its Access Token, Access Token Secret, Consumer Key, and Consumer Secret. They can be found in the Advanced Settings under the Twitter section. The Authentication is done in the Application_Start of the Global.asax file: 

protected void Application_Start(object sender, EventArgs e)
       {
           TwitterCredentials.ApplicationCredentials = TwitterCredentials.CreateCredentials("Access_Token",
               "Access_Token_Secret",
               "Consumer_Key",
               "Consumer_Secret");
  
       }

This is all there is to it. From then on your tweets will be send via the account associated with this application.  

Next we need to intercept the publishing of the content items we would like to tweet. For this purpose I have used Sitefinity's EventHub. More precisely I attached to the IDynamicCreatedEvent. A simple check is made there based on the type of the item and its status. The important thing here is the value of the Yes/No field. This will determine whether to tweet the item or not:.

protected void Application_Start(object sender, EventArgs e)
       {
           TwitterCredentials.ApplicationCredentials = TwitterCredentials.CreateCredentials("Access_Token",
               "Access_Token_Secret",
               "Consumer_Key",
               "Consumer_Secret");
 
 
           Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
       }
 
       public static void Bootstrapper_Initialized(object sender, ExecutedEventArgs args)
       {
           if (args.CommandName == "Bootstrapped")
           {
               EventHub.Subscribe<IDynamicContentCreatedEvent>(IDynamicContentCreatedEvent);
           }
       }
 
       private static void IDynamicContentCreatedEvent(IDynamicContentCreatedEvent eventInfo)
       {
           DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
           //var dynamicContentItem = eventInfo.Item;
           Type samplecontentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.SampleModules.Samplecontent");
           if (eventInfo.Item.GetType() == samplecontentType)
           {
               var Item = dynamicModuleManager.GetDataItem(samplecontentType, eventInfo.Item.Id);
               var fieldValue = Item.GetValue("ShouldTweet");
               var content = Item.GetValue("Content");
 
               if (Item.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master && fieldValue.ToString() == "True")
               {
 
                   // Publish the content as tweet
                   var tweet = Tweet.PublishTweet(content.ToString());
 
               }
           }
  
       }

This is all that is needed. At this point you will tweet the content of your items based on the field value.

I hope you find the above sample interesting and useful. As always your opinion is really important to us, so please feel free to let us know what you think if you have any comments/ideas.

Ivan D.Dimitrov

View all posts from Ivan D.Dimitrov on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation