all posts

Customizing Graffiti Chalk GetQuerystringValue

Published to Blog on 27 Jan 2009

My last Customizing Graffiti post was a simple little thing. This one is just as simple - the GetQuerystringValue Chalk extension. I had a need for pulling a value from the querystring recently and using it my view markup. I was sure that something to do that must exist in Graffiti, but I looked and looked through the Graffiti docs and forums and came up empty.

What does it do?

You use it to pull a value out of Querystring parameter. That’s it. I’ve found this to be really useful to pull in a querystring value for use as a parameter in another Chalk extension method call.

The Code

namespace DanHounshell.Graffiti.Chalk
{
  [Chalk("QueryStringHelper")]
  public class QuerystringHelper
  {
    public string GetQuerystringValue(string key)
    {
      if (HttpContext.Current.Request.QueryString[key] != null)
      {
        return HttpContext.Current.Request.QueryString[key];
      }
      return string.Empty;
    }
  }
}

Usage

Just place the following wherever you need it in your view markup:

$QuerystringHelper.GetQuerystringValue("SomeQuerystringKey")

An entirely fictional “real world” example: I have a chalk extension that uses the Ebay API to get a list of the top 5 “going soon” auctions that match a specific keyword. I’m thinking about adding this to the bottom of every post on my site by including it in my post.view markup. The chalk method call inside my view pages might look like this:

$EbayService.GetAuctions("some keyword", 5)

I could always hardcode a keyword into the method call, but that wouldn’t be very useful. Or I could create a custom field for each post that is used as the keyword for the method call when that post is displayed. That might be a little better and could serve as the default. But let’s go a little further. If someone searched for a term on my Graffiti site, wouldn’t it be cool if I could use the term that they used to pull back Ebay auctions that match that term. I could see that being useful!

So in my index.view file I use conditionals to determine if it is displaying search results and if so then I append a querystring value like “searchedfor=thekeyword” to the Url of posts displayed in the search results. Then in my post.view I add something like this:

#if ($QuerystringHelper.GetQuerystringValue("searchedfor") != "")
  $!EbayService.GetAuctions($QuerystringHelper.GetQuerystringValue("searchedfor"), 5)
#else
  $!EbayService.GetAuctions($!post.Custom("EbaySearchTerm"), 5)
#end

When they click on a link in the search results they go to a post with the querystring appended. Now I can display items that match the search term they used previously or default to a custom term that I specified when I wrote the post if none is present.

Okay, I admit I took the long way around to create a complex contrived example to demo a piece of code that is so simple, but I promise that I will post a really good example that makes use of all three of my recent Graffiti tips later this week. 

Notes

After demoing this code to Jayme, he said something like “We already have that in Graffiti, but it is not documented very well”. He said that you can do the same thing with something like $request.QueryStringKey (where QueryStringKey = key you are trying to get the value of). There’s nothing like re-inventing the wheel!

Be careful, the above extension returns the raw querystring value without any html encoding or parsing of bad stuff. Make sure you take any necessary precautions before writing the value out to HTML, passing it to a database, etc.

I hope you find this Graffiti Chalk extension tip useful. Please leave a comment if you have any suggestions or questions.


Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, motorcycles, sports, chickens, watches, food, Nashville, Savannah, Cincinnati and family.
Dan Hounshell on Twitter