Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 
    //webPartManager.SaveChanges(pvwp); 
} 

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.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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";

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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