StackOverflow questions on your blog - Part 1

Sample questions using tags 'c#' & 'reflection'

The StackExchange API exposes several rest endpoints that lets us tap into one of the best technical resources around. We'll use the search method that will give us related questions once we pass a few parameters.

Using the metaWeblog api we’ll add the widget to every blog post.

Consuming the Stack Overflow data

The api is primarily focused in JavaScript applications. For those there is an oAuth authentication & key submission with every request.

Rather than retrieving the links with JavaScript we took the offline approach. This way you can process the links just one time, you won’t run into throttling issues and keep page loads fast.

C# : StackExchange API : Search

The Stack Exchange api was really straight forward to use. The only 'issue' was decompressing the gzipped response. Initially I tried to set request headers to not accept compression.

This approach will not work as documented here: Decompressing Stack Exchange API Responses. Thankfully the .net Framwework came to the rescue with the GZipStream class.

The sample below will be used to get questions for every post.

ReadFully method

Creating the widget

Sample response from StackExchange API 2.0 search method
Sample questions using tags stackoverflow-api' & 'search'

The response form this call is JSON data. We’ll use to parse it and create a html widget with the returned questions. At this stage we will only use the title and link of every question to display them as a list.

C# : StackExchange html widget

In the next post we'll use the metaWeblog api to insert the widget in blog posts. In particular we will take on parsing html and inserting/replacing the widget in the post markup.

C# : Full code

Below is the full script which you can run in the IF Workbench

comments powered by Disqus


Under Construction

About Me

Martin here. I'm a developer by choice. Focused in SharePoint & JavaScript.

  • Follow me on Twitter