Retrieve Query String in Page Viewer Web Part

Retrieve Query String in Page Viewer Web Part

There are many times that we create a SharePoint web part in which we have to pass query string parameters to it from the main page, or possibly use an aspx page. If the web part that we create resides inside a Page Viewer Control, we are unable to use the Request.QueryString method in order to retrieve those values.

In such case, we would have to use the Request.UrlReferrer property in order to retrieve the Query String of the parent page and manipulate the data using a Custom “Get Query String” method that we create.

There are many times that we create a SharePoint web part in which we have to pass query string parameters to it from the main page, or possibly use an aspx page. If the web part that we create resides inside a Page Viewer Control, we are unable to use the Request.QueryString method in order to retrieve those values.

In such case, we would have to use the Request.UrlReferrer property in order to retrieve the Query String of the parent page and manipulate the data using a Custom “Get Query String” method that we create.

The first code sample below shows how to use the Request.UrlReferrer.Query property to retrieve the Query String of the SharePoint page in the Page Load method of the SharePoint web part.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.UrlReferrer.Query))
                {
                    Guid eId = Guid.Empty; int ec = 0;
                    if (GetQueryString(Request.UrlReferrer.Query, ref eId, ref ec))
                    {
                        PreloadData(eId, ec);
                    }
                }
            }
        }

The second code sample below show how to use the custom GetQueryString method to retrieve the values from the Query string.

        private bool GetQueryString(string query, ref Guid entityId, ref int etc)
        {
            int iPos = query.IndexOf('?');
            if (iPos == -1)
            {
                return false;
            }
            else if (iPos >=0)
            {
                query = (iPos < query.Length - 1) ? query.Substring(iPos + 1) : string.Empty;
            }

            NameValueCollection coll = HttpUtility.ParseQueryString(query);
            foreach (string s in coll.AllKeys)
            {
                string key = s;
                string value = coll[s];
                switch(key)
                {
                    case "eid":
                        entityId = new Guid(value);
                        break;
                    case "ec":
                        etc = Convert.ToInt32(value);
                        break;
                    default:
                        break;
                }
            }

            return true;
        }

The above has been often requested for many of our SharePoint development projects.