Integrating PBWiki with .Net Using the New PBWiki API, The Start of a .Net API

Abstract

In this article we will demonstrate a technique used to automatically add Wiki Pages to PBWiki using
the not quite released API provided by the peanut butter guys. The demonstration will involve integrating scheduled presentations at our Silicon Valley Code Camp on October 7th and 8th each with its own Wiki Page. Without the API, creating and linking the presentation to a Wiki page would have to be an ugly manual process involving the dreaded cut and pasting of URL’s.

Background

So, to begin with, I’m an enterprise database kind of guy and to be frank, Wiki’s scare me a little. The
idea of adhoc data being deposited by anyone onto a page with no real structures just makes me nervous. My
smart friends tell me I should embrace the chaos so I’m trusting them and going for it.

That being said, the specific problem we want to address with the Wiki is to allow presenters and presentees at
our codecamp to be able to interact with each other before and after their presentations. My experience is that
discussion groups don’t work that well for this and though lotus notes may have, that doesn’t seem to be much of an
option these days (Where is Ray?). The now obvious solution is to attach a Wiki Page to each presentation on our web
site and let users and presenters have at it. The problem is that this event is free and we don’t have an army
of staffers ready to add Wiki Pages each time someone submits a new presentation.

PBWiki To The Rescue

Turns out the guys at PBWiki are just about to release an API for their software. One of the functions of this API allows us
to create a new Wiki Page simply by building a request with post data as our page. Just what we need. So, lets just go through the steps with some fancy pictures and arrows of how we built this integration. (OK, no arrows, just pictures).

First, lets take a look at how we’ve enhanced our web site. (OK, this is in beta so depending on how fast you are at reading this article, the web site might not be quite to production yet. You will need to use your imagination.)

Notice the Wiki hyperlink next to the Session Titles. When this hyperlink is pressed, an API call is generated to the PBWiki API calling the AddPage function. If the Wiki Page exists, it simply quietly fails. Then after that, a redirect is processed that takes the user directly to the new Wiki Page. First time, the Wiki page is created with all kinds of information about the session as well as any other information we might want to transfer over. Below is what the Wiki Page looks like on our CodeCamp Wiki.

The Plumbing

So, how did we do this? First, we got an API key from PBWiki. this is a huge 15 character or so alpha string that identifies your wiki so that when you use it, pages and such will be created on your wiki. Then, we had to write some code. Basically, the way the API works is that you build a request that looks something like this to add a page.

http://YOURWIKI.pbwiki.com/api/AddPage?apikey_v1=12345&page=SomeNewPage&name=Some%20Guy&email=someone@example.org

Then, you add some post data to the request (or for small pages you can use the data parameter). Execute the request and your response is that status of the request. It’s returned in JSON format. Hopefully, you have your page now.

After that, you simply issue a redirect to the newly create page.

http://YOURWIKI.pbwiki.com/SomeNewPage

Now, you are probably wondering, where is the .Net API for doing this? OK, I’ll put some code below to satisfy that
wonderment. Basically, I created a very small API (1 call, AddPage), and a simple aspx page to run it. Here it is, and good
luck. Overtime, I’m sure I’ll fill out the API and add real error checking and better return type stuff. But, for now, here it is.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
 
/// <summary>
/// An API for calling the PBWiki's API.  This is all C# and .NET 2.0
/// </summary>
public  class PBWikiAPI
{
 
    public static string AddPage(string wikiURL,string apiKey,string page, string email, 
        string name, string postData)
    {
        string responseReturn = string.Empty;
 
        try
        {
            string wikiPageName = Regex.Replace(page, @"[^w@-]", "");
            if (!String.IsNullOrEmpty(wikiPageName))
            {
                string url = wikiURL + "/api/AddPage?apikey_v1=" + apiKey
                                + "&page=" + wikiPageName
                                + "&name=" + "AutoGenerator"
                                + "&email=" + email;
                Uri ourUri = new Uri(url);
 
                // Create a 'WebRequest' object with the specified url. 
                WebRequest myWebRequest = WebRequest.Create(url);
                myWebRequest.Method = "POST";
 
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(page);
                myWebRequest.ContentLength = bytes.Length;
 
                System.IO.Stream os = myWebRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
                os.Close();
 
                // Send the 'WebRequest' and wait for response.
                WebResponse myWebResponse = myWebRequest.GetResponse();
                if (myWebResponse != null)
                {
                    System.IO.StreamReader sr1 = new System.IO.StreamReader(myWebResponse.GetResponseStream());
                    responseReturn = sr1.ReadToEnd().Trim();
                }
 
                // Rel
ease resources of response object.
                myWebResponse.Close();
            }
            else
            {
                throw new ApplicationException("PBWikiAPI: no Page Name Specified");
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("PBWikiAPI: " + ex.ToString());
        }
 
        return responseReturn;
    }
 
}

The Sample ASP.NET aspx Page that calls this

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class Default12 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string str = PBWikiAPI.AddPage("http://codecamp.pbwiki.com",
            "cd884b9f9d3034598340958435093405834",
            "TestPageFromAPI", "email", "name", "PostDataHere");
    }
}

Conclusions

This is very cool stuff. It opens up a huge area of how to synchronize (or somewhat synchronize) blog data and more structured

data from things like databases (what a concept!). I’m sure as time goes on, I’ll do much more sophisticated integration with

Wiki data then is shown here. I’m totally jazzed by this.

 

References

http://api.pbwiki.com/

http://pbwiki.com/

http://www.siliconvalley-codecamp.com/

About Peter Kellner

Follow me:


Comments

  1. to get a truly different trip you will probably like to look at Russia, it may be a vast country with a incredibly diverse population. It is rich with culture and varied scenery.

  2. Here’s a similar but slightly more full featured pbwiki api wrapper, also in c#…

    http://groups.google.com/group/pbwiki-api/browse_thread/thread/2b16c532f8b088ce

Your Comments

*

Protected with IP Blacklist CloudIP Blacklist Cloud

Follow

Get every new post delivered to your Inbox

Join other followers: