Skip to content
August 4, 2012 / red1s

Quick Tip – programmatically adding a pageviewer webpart to a page


Just thought I’d create a quick post on adding a page viewer webpart (or any other webpart ) to a page:

The following code can be dropped into a webpart class to get a webpart to be programmatically be placed on the page with a few of the variables set:


//Construct url ———————–

string strScorecard = Page.Request.QueryString["scorecard"].ToString();

string url = "http://cognosserver/?scorecard=" + strScorecard + "%20default%20pdf-en-ie.pdf";

// string url = "" + division;

//Construct url ———————–

// SPWeb myWeb = SPContext.Current.Web;

myWeb.AllowUnsafeUpdates = true;

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager wpcWebParts = null;

// wpcWebParts = myWeb.GetLimitedWebPartManager("http://connecttest/projects/Calendar/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

wpcWebParts = myWeb.GetLimitedWebPartManager("http://connecttest/Pages/MetricsDashboard.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

//wpcWebParts = myWeb.GetLimitedWebPartManager(pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

foreach (System.Web.UI.WebControls.WebParts.WebPart wptWebPart in wpcWebParts.WebParts)


PropertyInfo[] pinProperties = wptWebPart.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo pinProperty in pinProperties)


lblError.Text = pinProperty.Name.ToString();

if (pinProperty.Name == "/forest/test.aspx")


// pinProperty.SetValue(wptWebPart, url, null);

pinProperty.SetValue(wptWebPart, url, null);







Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: