Emancipate yourself from mental slavery; none but ourselves can free our minds. – Nesta

So, as I was working away, setting up a Links list in SharePoint to take advantage of the QueryStringPageViewer Web Part discussed in my last blog post, I came upon a situation where I needed to do something slightly different.

The QueryStringPageViewer Web Part allows a user to configure the Web Part with a Base URL attribute, then fills up the web part’s IFRAME by concatenating the Base URL with the query string of the web part page, using Page.Request.QueryString.  The best use for this web part is when you have a bunch of pages you want to link to on a different server, where the URL format of those pages looks something like this:  http://intranet.company.com/Default.aspx?pageid=4563

But what about a situation where the existing pages are all distinct, such as:  http://intranet.company.com/Finance.aspx, http://intranet.company.com/HR.aspx, http://intranet.company.com/Manufacturing.aspx, etc.?  Let’s say you had 40 of those pages that you wanted to expose within SharePoint in an effort to incorporate existing content sources.  If you were going to use the standard PageViewer Web Part, you will need to create 40 separate Web Part Pages, each with a PageViewer Web Part wired up to the URL of one of the 40 pages.

So, what about making a generic web part that uses a query string parameter from the web part page, a la my previous blog post referenced above, to dynamically set the source of a PageViewer web part?  What if you could deploy that simple web part on a “generic” web part page and reference it anywhere in your SharePoint implementation that you need to use a PageViewer web part?  Would you rather have 40 instances of the PageViewer web part in your environment or one instance of this new web part, called the DynamicPageViewer web part?

As you would expect, the code is very similar to the code from my last blog post, as we will be using the Page.Request.QueryString object once again.  Here are the steps you need to take to change that code to perform this new function.  More detailed instructions on creating the project, deploying the web part, etc., can be found in the last blog post.

Here are the steps required:

with this:

NameValueCollection queryString = Page.Request.QueryString;
string URL = "";
if (queryString.Count > 0)
{
    // Loop through QueryString collection
    foreach (string qsName in queryString)
    {
        if (qsName.ToLower() == "url")
            URL = queryString[qsName];
    }
}

See?  I told you it was super simple!

Once you get the web part deployed and on a web part page, type in any random query string at the end of the URL for the web part page, making sure to include a parameter called url that actually contains a valid URL (note the use of "ToLower()" in the code, so url, URL and Url are all supported). For example, replace http://intranet.company.com/TestSite/Pages/WebPartTest.aspx with http://intranet.company.com/TestSite/Pages/WebPartTest.aspx?pageid=1999&red=green&url=http://autos.msn.com&napster=bad

The custom web part should be blank, and then when you add the query string with the url parameter, it will fill up with the MSN Autos page.

ADDENDUM:

In certain situations, you will need to use url escape codes in the ‘url’ parameter.  For instance, if the URL you are targeting has a query string, you need to use the escape code for the “question mark” (%3F) to start the query string and use the escape code for the “ampersand” (%26)to delimit the parameters.  If you fail to use the escape codes, you will not achieve the desired results.  Using the escape code for “equals” (%3D)is not required, although it might make sense to adopt a standard convention where you use the escape code for all eligible characters within the ‘url’ parameter.

Comments

Leave a Reply