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:
- Get the code from the previous version, where we use the entire query string. It is located here: http://www.themackpage.com/files/Dynamic-PageViewer-Web-Part.txt
- Find and remove this code:
//Set custom attribute to allow user to specify Base URL in Tool Pane private string _baseURL = ""; [WebBrowsable(true), Personalizable(PersonalizationScope.Shared), Category("Base URL Settings"), WebDisplayName("Base URL"), WebDescription("Supply the Base URL for the page viewer query string web part.")] public string baseURL { get { return this._baseURL; } set { this._baseURL = value; } } - In the RenderWebPart method, replace this code:
NameValueCollection queryString = Page.Request.QueryString; string URL = this.baseURL; int paramCount = 0; if (queryString.Count > 0) { // Set up URL as having a Query String URL += "?"; // Loop through QueryString collection and add all name/value pairs to 'URL' foreach (string qsName in queryString) { // Add name/value pair for current key to the URL URL += qsName + "=" + queryString[qsName]; // Add ampersand for all but the last query string parameter paramCount++; if (paramCount < queryString.Count) URL += "&"; } }
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.

Posted in 


Great solution, thanks for sharing. I have successfully implemented this webpart in Sharepoint 2010.
Unfortunately when I try these out I get a scrollbar on the right side no matter what I do. With OOTB page viewers I can overcome this by putting a dimension.
Ideas?
Very useful webpart. I deployed in sp2010 following your instructions and it worked fine. But, I have one gripe: the webpart is rendering with an extra scrollbar that has no effect because it is almost the same size as the part. This scroll bar appears whenever the webpart is given a custom height. If anyone else has noticed this and has a fix I would be interesteed to hear about it.
Very well done. I just stumbled upon this post as I was working on my sharepoint site and found this webpart incredibly useful. As for the extra scrollbar rendering with it, I found a simple workaround.
Simply change the 100% in the following two lines to 99% and it worked for me:
output.AddAttribute(HtmlTextWriterAttribute.Width, “100%”, false);
output.AddAttribute(HtmlTextWriterAttribute.Height, “100%”, false);
Again thanks for the post. Incredibly useful tool.
What if I need a dynamic pageviewer webpart only for my subsite..I don’t have permissions to the MOSS server..is it possible?
Hi
really useful. I have implemented this now. When I enter the first page ( of a legacy servlet system ) , everything is fine albeit the iFrame seems to overwrite some of the header of my web part page. However, when I click on the button in in my legacy system I get browser error and there is no navigation to the next page.
I have test this link in IE with a standard link (new browser window) and it’s ok.
Oh I meant to say the error I get in the browser is
“can’t load XRegExp twice in the same frame”
Hi
Just to let you know … I url enc all my links before passing to the web part
document.write(new String(“Daniel’s link”).link(new String(“/SitePages/MYDynamicPageViewer.aspx?url=”).concat(escape(“http://myBackendSytemlink” )) ) );
Not elegant but this does work.