GraffitiClient API: calling GraffitiCMS from a program

I have a whole set of posts on my static website that I'd like to transfer over to my Graffiti CMS site, leaving behind a redirect link. Since I was using CityDesk from Fog Creek to blog before, there's no migration tool available. Hey, no problem, I'm a developer at heart so it's just a simple case of opening up a a couple of databases, writing a conversion routine, and Bob's your uncle.

Even better, Graffiti exposes a web service that I can call. I only need to open ONE database, and the conversion method calls a method on the web service to add these converted posts! Brilliant. Sometimes I astound myself with my perspicacity.

But, boy finding out information about the web service is ruddy difficult and I ran into some issues when I did so. This post is a quick explanation of what you should do in order to use the Graffiti web service.

First of all, when you unzipped the Graffiti product you would have created three folders: Data. Utility, and Web. The latter you will have copied to your web site. In Utility there's a dll called GraffitiClient.dll which is the entryway to the Graffiti web service. (Yeah, I'm being pedantic here about where it is because at one point I was looking in web/_utility on the web site for this mysterious dll. Grasshopper, do not follow my path.)

In Visual Studio, create a new Console application. Add a reference to GraffitiClient.dll. Type the basic code to list the categories (the code shown is for this blog). By the way, do not miss the api folder name off the end of the URL.

using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraffitiClient.API;

namespace GraffitiApiTest {
  class Program {
    static void Main() {
      Console.WriteLine("List of categories");

      GraffitiService gs = new GraffitiService("username", "password", "https://boyet.com/api/");

      try {
        PagedList<Category> plc = gs.Categories.Get(new NameValueCollection());

        foreach (Category c in plc)
          Console.WriteLine(c.Name);
      }

      catch (GraffitiServiceException gse) {
        Console.WriteLine(gse.Message);
        Console.WriteLine(gse.StatusCode);
      }

      Console.WriteLine("..done");
      Console.ReadLine();
    }
  }
}

(Obviously, those aren't valid user credentials for my website.) I decided to create a "special" user in Graffiti to handle access through the web service rather than use my main one. So I created a user called MigrationUser, a contributor, changed the hard-coded user and password in the code above, compiled and ran the application.

Boom.

Thread was being aborted
500

What the... ? So I dutifully looked up error 500 and essentially all I could find was "The server ran into a problem. Tough. Talk to your system admin." Couldn't find anything relevant about "Thread was being aborted", although there was some muttering about timeouts. I went to the Graffiti logs: nothing there at all. Hmm.

I pulled out Reflector and started looking through the code paths. This is made slightly more tricky since there are two of course: one on the client and one on the server. I was guessing that it was the server one that was the most probable, but the only error 500 I found was in a bit of code for deleting a category. Didn't sound right. I did notice some code that talked about permissions; hmm, I wonder.

So I made MigrationUser an admin.

And the code worked.

There you have it: the 5 minute intro to GraffitClient.dll. Oh and make sure the user whose credentials you are using is an admin.

Now to write a migration tool.

Album cover for Greatest Hits Now playing:
Ace of Base - All That She Wants
(from Greatest Hits)



Loading similar posts...   Loading links to posts on similar topics...

No Responses

Feel free to add a comment...

Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response