I am trying to find a way to add Page Viewer webpart programatically and display the website in it...., i have a requirement where i will pass the url to display in the page viewer... below is the code... does not work....

ps: i will be have the url from db just for now i just hard-coded to www.cnn.com to for testing purpose...

using (SPSite site = new SPSite("http://servername/sites/test/")) 
    SPWeb web = site.OpenWeb(); 
    SPFile webPartPage = web.GetFile("www.cnn.com"); //NOT SURE ??????  
    SPLimitedWebPartManager webPartManager = webPartPage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); 
    PageViewerWebPart pvwp = new PageViewerWebPart(); 
    pvwp.Title = "My Page Viewer Web Part"; 
    webPartManager.AddWebPart(pvwp, "TOP", 0); 

    // not actually needed 

i get this error: he Web application at http://www.cnn.com could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.


With your code you are trying to retrieve the file "www.com.com" from your local SharePoint web with the GetFile method of the current web. Obviously that will fail. You need to fill in the url of the page to which you want to add the webpart to (e.g. default.aspx).

To set the url that PageViewerWebPart shows, set the ContentLink property of the webpart pvwp.ContentLink = "http://www.cnn.com";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.