Using Query Strings

Using Query Strings

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.SafeDictionary<T>
  • Sitecore.Data.Items.Item
  • Sitecore.Web.WebUtil
  • System.Collections.Generic.KeyValuePair<T,T>
  • System.String
  • System.Web.HttpUtility

Methods featured in this example:

  • Sitecore.Collections.SafeDictionary<string>
  • Sitecore.Web.WebUtil.AddQueryString()
  • Sitecore.Web.WebUtil.ExtractUrlParm()
  • Sitecore.Web.WebUtil.GetFullUrl()
  • Sitecore.Web.WebUtil.GetPathAndQuery()
  • Sitecore.Web.WebUtil.GetQueryString(()
  • Sitecore.Web.WebUtil.GetRawUrl()
  • Sitecore.Web.WebUtil.GetServerUrl()
  • Sitecore.Web.WebUtil.ParseQueryString()
  • System.Collections.Generic.KeyValuePair<string, string>
  • System.String.IsNullOrEmpty()
  • System.Web.HttpUtility.UrlDecode()
  • System.Web.HttpUtility.UrlEncode()

Query Strings C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

string rawText1 = "The Inside Corner, Inc.";
string rawText2 = "Benicia, CA 94510";
string rawText3 = "(707) 319-7446";

string qString1 = System.Web.HttpUtility.UrlEncode(rawText1);
string qString2 = System.Web.HttpUtility.UrlEncode(rawText2);
string qString3 = System.Web.HttpUtility.UrlEncode(rawText3);
string serverUrl = Sitecore.Web.WebUtil.GetServerUrl();
string rawUrl = Sitecore.Web.WebUtil.GetRawUrl();
string fullUrl = Sitecore.Web.WebUtil.GetFullUrl(rawUrl, serverUrl);
//"serverUrl + rawUrl" is equivalent to fullUrl
string url1 = Sitecore.Web.WebUtil.AddQueryString(serverUrl + rawUrl, "comp", rawText1, "loc", rawText2, "ph", rawText3);
string url2 = Sitecore.Web.WebUtil.AddQueryString(fullUrl, "comp", qString1, "loc", qString2, "ph", qString3);

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", displayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Raw Text 1", "<b>" + rawText1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Raw Text 2", "<b>" + rawText2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Raw Text 3", "<b>" + rawText3 + "</b><br/><br/>");
if (!System.String.IsNullOrEmpty(qString1))
    output.Write("{0}&#58;&#160;{1}", "Query String 1", "<b>" + qString1 + "</b><br/>");
if (!System.String.IsNullOrEmpty(qString2))
    output.Write("{0}&#58;&#160;{1}", "Query String 2", "<b>" + qString2 + "</b><br/>");
if (!System.String.IsNullOrEmpty(qString3))
    output.Write("{0}&#58;&#160;{1}", "Query String 3", "<b>" + qString3 + "</b><br/><br/>");

if (!System.String.IsNullOrEmpty(serverUrl))
    output.Write("{0}&#58;&#160;{1}", "Server URL", "<br/><b>" + serverUrl + "</b><br/><br/>");
if (!System.String.IsNullOrEmpty(rawUrl))
    output.Write("{0}&#58;&#160;{1}", "Raw URL", "<br/><b>" + rawUrl + "</b><br/><br/>");
if (!System.String.IsNullOrEmpty(fullUrl))
    output.Write("{0}&#58;&#160;{1}", "Full URL", "<br/><b>" + fullUrl + "</b><br/><br/>");

string pathQuery = Sitecore.Web.WebUtil.GetPathAndQuery(url2);
if (!System.String.IsNullOrEmpty(pathQuery))
    output.Write("{0}&#58;&#160;{1}", "Path and Query for URLEncoded URL", "<br/><b>" + pathQuery + "</b><br/><br/>");
if (!System.String.IsNullOrEmpty(url1))
    output.Write("{0}&#58;&#160;{1}", "Query String without UrlEncode", "<br/><b>" + url1 + "</b><br/><br/>");
if (!System.String.IsNullOrEmpty(url2))
    output.Write("{0}&#58;&#160;{1}", "Query String with UrlEncode", "<br/><b>" + url2 + "</b><br/><br/>");

string qString = Sitecore.Web.WebUtil.GetQueryString();
Sitecore.Collections.SafeDictionary<string> queryString = Sitecore.Web.WebUtil.ParseQueryString(qString);

output.Write("{0}", "<br/><hr/><br/>");
if (!System.String.IsNullOrEmpty(qString))
{
    //pass query string to this method
    queryString = Sitecore.Web.WebUtil.ParseQueryString(qString);
    if (queryString != null)
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
        output.Write("{0}&#58;", "Query String Key/Value Pairs");
        output.RenderEndTag(); //P
        foreach (System.Collections.Generic.KeyValuePair<string, string> qStr in queryString)
        {
output.Write("{0}&#58;&#160;{1}", "Key", "<b>" + qStr.Key + "</b>  ");
output.Write("{0}&#58;&#160;{1}", "Value", "<b>" + qStr.Value + "</b><br/>");
        }
        string qsParse1 = queryString["comp"];
        string qsParse2 = queryString["loc"];
        string qsParse3 = queryString["ph"];

        if (!System.String.IsNullOrEmpty(qsParse1))
output.Write("{0}&#58;&#160;{1}", "<br/>Query String Parse for Company Name", "<b>" + qsParse1 + "</b><br/>");
        if (!System.String.IsNullOrEmpty(qsParse2))
output.Write("{0}&#58;&#160;{1}", "Query String Parse for Location", "<b>" + qsParse2 + "</b><br/>");
        if (!System.String.IsNullOrEmpty(qsParse3))
output.Write("{0}&#58;&#160;{1}", "Query String Parse for Phone", "<b>" + qsParse3 + "</b><br/><br/>");

        //pass query string key and URL to this method instead of passing query string
        string urlParm1 = Sitecore.Web.WebUtil.ExtractUrlParm("comp", Sitecore.Web.WebUtil.GetRawUrl());
        string urlParm2 = Sitecore.Web.WebUtil.ExtractUrlParm("loc", Sitecore.Web.WebUtil.GetRawUrl());
        string urlParm3 = Sitecore.Web.WebUtil.ExtractUrlParm("ph", Sitecore.Web.WebUtil.GetRawUrl());

        if (!System.String.IsNullOrEmpty(urlParm1))
output.Write("{0}&#58;&#160;{1}", "URL Parameter for Company Name", "<b>" + urlParm1 + "</b><br/>");
        if (!System.String.IsNullOrEmpty(urlParm2))
output.Write("{0}&#58;&#160;{1}", "URL Parameter for Location", "<b>" + urlParm2 + "</b><br/>");
        if (!System.String.IsNullOrEmpty(urlParm3))
output.Write("{0}&#58;&#160;{1}", "URL Parameter for Phone", "<b>" + urlParm3 + "</b><br/><br/>");

        string compDecode = System.Web.HttpUtility.UrlDecode(Sitecore.Web.WebUtil.GetQueryString("comp"));
        string locDecode = System.Web.HttpUtility.UrlDecode(Sitecore.Web.WebUtil.GetQueryString("loc"));
        string phDecode = System.Web.HttpUtility.UrlDecode(Sitecore.Web.WebUtil.GetQueryString("ph"));
        if (!System.String.IsNullOrEmpty(compDecode))
output.Write("{0}&#58;&#160;{1}", "Decoded Company Name", "<b>" + compDecode + "</b><br/>");
        if (!System.String.IsNullOrEmpty(locDecode))
output.Write("{0}&#58;&#160;{1}", "Decoded Location", "<b>" + locDecode + "</b><br/>");
        if (!System.String.IsNullOrEmpty(phDecode))
output.Write("{0}&#58;&#160;{1}", "Decoded Phone Number", "<b>" + phDecode + "</b><br/><br/>");
    }
}
else
    output.Write("{0}", "<b>Please paste the URL under the 'Query String with UrlEncode' heading into the Address Bar and refresh the Browser display to complete this demo.</b>");

output.RenderEndTag(); //Div


Query Strings C# Web Control Output

Raw Text 1: The Inside Corner, Inc.
Raw Text 2: Benicia, CA 94510
Raw Text 3: (707) 319-7446

Query String 1: The+Inside+Corner%2c+Inc.
Query String 2: Benicia%2c+CA+94510
Query String 3: (707)+319-7446

Server URL: 
http://tic66

Raw URL: 
/en/Developers/ContentDisplay/LinkManagement/UsingQueryStrings

Full URL: 
http://tic66/en/Developers/ContentDisplay/LinkManagement/UsingQueryStrings

Path and Query for URLEncoded URL: 
/en/Developers/ContentDisplay/LinkManagement/UsingQueryStrings?comp=The%2bInside%2bCorner%252c%2bInc.&loc=Benicia%252c%2bCA%2b94510&ph=(707)%2b319-7446

Query String without UrlEncode: 
http://tic66/en/Developers/ContentDisplay/LinkManagement/UsingQueryStrings?comp=The+Inside+Corner%2c+Inc.&loc=Benicia%2c+CA+94510&ph=(707)+319-7446

Query String with UrlEncode: 
http://tic66/en/Developers/ContentDisplay/LinkManagement/UsingQueryStrings?comp=The%2bInside%2bCorner%252c%2bInc.&loc=Benicia%252c%2bCA%2b94510&ph=(707)%2b319-7446


Please paste the URL under the 'Query String with UrlEncode' heading into the Address Bar and refresh the Browser display to complete this demo.