Wednesday, May 2, 2012

Building a Twitter Client In InfoPath


Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories:InfoPath; Twitter; Site Manager/Power User; MOSS; WSS; 2007; 2010

InfoPath 2010 introduced the ability to get external data from a “REST Web Service.”’ REST (REpresentational State Transfer) services became the darling of Web 2.0 a few year s back when Facebook exposed their API as a ‘RESTful’ service. Every other major site has since followed suit and we now have the ability to deeply integrate our projects with the social experience.
The exact definition of a RESTful system has become a source of contention among geeks. Proponents of ‘pure’ REST architecture point to Roy Fielding’s 2000 PhD dissertation. They cite things like reuse of HTTP verbs, meaningful headers, stateless protocols and identifiable resources. This group would be shocked at the InfoPath implementation. InfoPath has no idea what a header is, can only make GET requests, and you can return data in any format you want, as long as it’s XML.
I however, ask myself one simple question when deciding if I can call a system RESTful. ‘Can I hook that up to Twitter?’
We begin with a new blank form, and add a REST Web Service from the Data tab.
2011-07-26-BuildingTwitterClient-01.png
Compared to the SOAP equivalent, the REST Web Service settings are very simplistic. Simply add a URL to receive data from, and save the data connection. In this example we will point InfoPath to http://search.twitter.com/search.rss?q=twitter, and give it a name.
2011-07-26-BuildingTwitterClient-02.png
 2011-07-26-BuildingTwitterClient-03.jpg
This URL can be broken into three distinct parts
  • search.twitter.com – the base URL that hosts the Search API
  • search.rss - this specifies that we want our response formatted as an RSS feed. ATOM and JSON are also available, but ATOM doesn’t have all the fields we need, and InfoPath doesn’t understand JSON.
  • ?q=infopath – here we specify what we want to search for in our query sting using the q parameter. We’ve chosen to search for “twitter” by default.
After saving the connection you’ll have a new Secondary Data Source.
2011-07-26-BuildingTwitterClient-04.jpg
Drag the item repeating group onto your form as a Repeating Section with Controls, and after expanding all the fields, preview your form.
2011-07-26-BuildingTwitterClient-05.jpg
2011-07-26-BuildingTwitterClient-06.jpg
If you receive an error when you preview the form, go under File > Advanced Form Options > Security and Trust and set the form to Full Trust mode. This certainly isn’t a best practice, but it get’s around InfoPaths reluctance to make cross domain calls in the simplest way possible.
This is neat, but what about if you want to search for something other than ‘twitter’? Users of SOAP web services are familiar with setting a query field in a data source, but REST Web Services are handled a little differently. Start by adding a field at the top of your page named keyword, and put a button under that. Click on the button and a new Control Tools contextual menu will appear. Click on Rules to open the buttons rules pane.
2011-07-26-BuildingTwitterClient-09.jpg
Under Add you have a new action for 2010, “Change REST URL”.
2011-07-26-BuildingTwitterClient-10.jpg
In this action, you will use the concat() function to build a formula that combines the text “http://search.twitter.com/search.rss?q=” and the value entered in the keyword field. Be careful when entering the URL, if you don’t enter the URL to the exact same RESTful resource, your data source will break. This is one of the drawbacks of the REST Web Service in 2010.
2011-07-26-BuildingTwitterClient-11.jpg
Add a second action to your rule to query the Twitter data source after modifying the URL. Without this the form won’t be refreshed with new data after you’ve clicked your search button.
2011-07-26-BuildingTwitterClient-12.jpg
Preview your form and try searching with a new value in the keyword field. I’ve made the Title field wider and multi-line to make it easier to read as well.
2011-07-26-BuildingTwitterClient-13.jpg
You now have a functioning Twitter search in InfoPath! However, there is one problem, it’s pretty ugly. To add a little extra visual appeal we’ll use another 2010 feature, the image control. Right click on the image_link field and change the control type to Picture.
2011-07-26-BuildingTwitterClient-15.jpg
I’ve also moved around some fields to make the tweet more readable, and but a border at the top and bottom of the repeating section to differentiate between tweets easier.
2011-07-26-BuildingTwitterClient-16.jpg
Preview your form and you have a sharp looking, enterprise-ready Twitter search.
2011-07-26-BuildingTwitterClient-17.jpg

Conclusion:

InfoPath 2010 brought with it the ability to make calls to RESTful web services. However, ’HTTP’ web service would be a better name for this feature as InfoPath doesn’t support most features that make a REST architecture so appealing. Nevertheless, it does open the door to some interesting opportunities. In this article you’ve seen how to use the Twitter search API to pull data from twitter, then design a form to query the service and display tweets using features like the image control.
Knowing how to use REST web services in InfoPath enables us to use all kinds of data sources. InfoPath developers are no longer restricted to web services with a valid WSDL files. We can now request any valid XML (and potentially even HTML) and parse the response. Sources with rich, URL based identifiers like SharePoint list oData feeds are among the data sources that are opened up thanks to RESTful web servies.
If you would like to see the full form, please visit http://twinfopath.codeplex.com

No comments:

Post a Comment