Sitecore API for Adding & Retrieving Query Strings

Posted 09/09/2016 by Subramanian Ramanathan

Whenever we start any new project we tend to write some Utilities class with frequently used methods like GetItem, EditItem, etc. and also, adding & retrieving query strings using UriBuilder/HttpUtility.ParseQueryString API.

As we know, Sitecore uses query strings like sc_mode, sc_lang etc.
This got me a thinking.. Sitecore should have come up with its own methods for adding & retrieving query strings.

Luckily we have such methods in Sitecore.Web.WebUtil class, syntax below,

Sitecore.Web.WebUtil.AddQueryString(url, key1, value1, key2, value2) 

AddQueryString method will encode the query string values, so url encoding using HttpUtility.UrlEncode is not required(in case of special characters). When decoding is required, ExtractUrlParm & ParseQueryString methods of WebUtil class will help. We can also have default values assigned while retrieving Query Strings.

Hope this will save you some lines of code and time.


Add your comment