R – PageViewer WebPart won’t load URL with GET parameters

asp.netsharepointweb-parts

I need to show an external website in my SharePoint portal so I have added a PageViewerWebPart. But the URL includes login parameters like www.mywebsite.com?login=X&passwd=Y.

I tried to add ASP code at the ContentLink PageViewer property like that :

<WebPartPages:PageViewerWebPart runat="server" ContentLink="<% ="URL" %>" ... />

It returns me this error : server tags cannot contain <% … %> instructions.

Does exists other properties which allow to load URL with GET parameters ?
Can I put this code into the CodeBehind of my WebPartPage ?
Am I actually doing it wrong… ?

Any help will be very apreciated !

Timothée Martin.

Best Answer

While I think it's a bad idea to put the credentials of a user in a URL (anyone will be able to see this by viewing the page's HTML source), here's how you could accomplish what you want to do:

Create a new class library project with a class that inherits from Microsoft.SharePoint.WebPartPages.WebPartPage. Something like:

namespace MyNameSpace.WebPagePages
{
    public class MyWebPartPage : Microsoft.SharePoint.WebPartPages.WebPartPage
    {
        protected Microsoft.SharePoint.WebPartPages.PageViewerWebPart myPageViewerWebPart;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // logic to create your URL
            string url = "http://blah/blah";

            // set the URL
            myPageViewerWebPart.ContentLink = url;
        }
    }
}

You'll need to sign the assembly and deploy it to the GAC. Using SharePoint Solution (WSP) is the recommended way to do this (but not the point of this answer).

You now need to modify the ASPX page containing your web part as follows:

  1. Change the Inherits attribute of the <%@ Page %> directive to the fully qualified type name of the above class. It will look something like: MyNameSpace.WebPagePages.MyWebPartPage, MyNameSpace.WebPagePages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b538f318242c0b01
  2. Give the web part an ID attribute with a value that matches the name of the web part in the code-behind class. In this example: <WebPartPages:PageViewerWebPart runat="server" ID="myPageViewerWebPart">

That should do it. If you've not done it before, creating the class and deploying it to the GAC will be the tricky bits, but they're easy when you know how. Tools like VSeWSS, STSDEV, and WSPBuilder (learn one first and then try the others) will help you accomplish this by creating WSP files (SharePoint Solutions) - something I highly recommend you take advantage of.