Thursday, July 3, 2014

Never ever use ID as a query string name in SharePoint 2010

A few weeks ago I was working on a SharePoint 2010 project for a client.  The project added blogging features to an existing site.  This would seem pretty simple because there were only two pages in the blog site collection.  Needless to say, I hit a lot of undocumented issues.  The first being that you can never ever use ID as a query string name. 

The Scenario
I was using the out of the box blog site template which I added two pages to with a feature receiver.  The first page was the home page for the site which displayed all of the blog posts.  The second, a post page, took a query string as the ID of the blog post, retrieved the blog post and displayed it to the user.  At first, I named my query string ID which was a good idea until there were more than two blog posts in the site.  If I clicked on a blog post with an id of 1 or 2 from the home page, the post page worked.  If the id was greater than 2, an exception was thrown by the page.  Can you guess why?

The Solution
The reason this occurred is because, in SharePoint, the ID query string is reserved and ALWAYS references the ID of the current page.  So, after digging around and showing other SharePoint ninjas was going on, I finally renamed the query string to blogId and it worked.

The Lesson
The lesson here is to never ever name a query string ID.  It should be more specific whether using SharePoint or any other development platform.