A place to share what I learn with everyone!
Todd BaginskiTodd Baginski

Connected Page Viewer Web Part

By Todd Baginski

This is a repost of an article on my blog that was lost during a server failure.  Historically, this has been one of the most popular articles on my blog, so I dug up the code and put it back online.

The code you will find below is for a Connected Page Viewer Web Part for WSS V2 and SPS 2003.  The Connected Page Viewer Web Part consumes a URL from another Web Part that provides URLs.  This Web Part WORKS GREAT in WSS V3 and MOSS 2007 as well.

Enjoy!

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Security;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebPartPages.Communication;
using System.Text;
using System.IO;

namespace Custom.SharePoint.WebParts
{
///


/// This Web Part displays the contents of a URL that is passed in from a provider web part.
///

[DefaultProperty("Text"),
ToolboxData("<{0}:ConsumerPageViewer runat=server>"),
XmlRoot(Namespace="Custom.SharePoint.WebParts")]
public class ConsumerPageViewer : WebPart , ICellConsumer
{
#region Properties

///


/// The URL to display in the IFRAME
///

private string pageUrl = string.Empty;
///
/// The property that exposes the pageUrl variable
///

[Browsable(true),
Category("Customization"),
DefaultValue(""),
WebPartStorage(Storage.Personal),
FriendlyName("Page Url"),
Description("Default page URL such as http://www.sharepointexperts.com")]
public string PageUrl
{
get {return pageUrl;}
set {pageUrl = value;}
}

///


/// Variable to toggle if the URL of the content in the web part is displayed.
///

private bool showPageUrl = false;
///
/// The property that exposes the showPageUrl variable
///

[Browsable(true),
Category("Customization"),
DefaultValue(false),
WebPartStorage(Storage.Shared),
FriendlyName("Show Page Url"),
Description("Specifies if the URL of the content in the web part is displayed.")]
public bool ShowPageUrl
{
get {return showPageUrl;}
set {showPageUrl = value;}
}
#endregion
// Event required by ICellConsumer
public event CellConsumerInitEventHandler CellConsumerInit;

// Keep a count of ICellConsumer connections.
private int cellConnectedCount = 0;

// Cell information
private string cellName = "Cell Data"

///


/// Register interfaces.
///

public override void EnsureInterfaces()
{
try
{
// Register the ICellConsumer interface.
RegisterInterface("CellConsumer_WPQ_",
"ICellConsumer",
WebPart.UnlimitedConnections,
ConnectionRunAt.Server,
this,
"CellConsumer_WPQ_",
"Consumes a cell from",
"Consumes a cell of data");
}
catch(SecurityException)
{
}
}

///


/// This method is called by the framework to determine whether a part
/// can be run on the client or server based on the current
/// configuration.
///

public override ConnectionRunAt CanRunAt()
{
return ConnectionRunAt.Server;
}

///


/// Notification to the Web Part that it has been connected
///

/// Unique name of the interface that is being connected
/// Reference to the other Web Part that is being connected to
/// Unique name of the interface on the other Web Part
/// Where the interface should execute
public override void PartCommunicationConnect(string interfaceName,
WebPart connectedPart,
string connectedInterfaceName,
ConnectionRunAt runAt)
{
EnsureChildControls();
if (interfaceName == "CellConsumer_WPQ_")
{
cellConnectedCount++;
}
}

///


/// In this method, a part should fire any connection init events.
///

public override void PartCommunicationInit()
{
if(cellConnectedCount > 0)
{
if (CellConsumerInit != null)
{
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
CellConsumerInit(this, cellConsumerInitArgs);
}
}
}

///


/// This method is called by the Authoring environment for all
/// the initial data required for creating a connection.
///

/// Name of interface that the framework is
/// requesting information on
/// An EventArgs object such as CellProviderInitArgs
public override InitEventArgs GetInitEventArgs(string interfaceName)
{
if (interfaceName == "CellConsumer_WPQ_")
{
EnsureChildControls();
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
return(cellConsumerInitArgs);
}
else
{
return(null);
}
}

///


/// The CellProviderInit event handler
/// The Provider part will fire this event during its PartCommunicationInit phase.
///

/// Provider Web Part
/// The args passed by the Provider
public void CellProviderInit(object sender, CellProviderInitEventArgs cellProviderInitArgs)
{
// This is where the Consumer part can identify what type of "Cell" the Provider
// will be sending.
}

///


/// The CellReady event handler
/// The Provider part will fire this event during its PartCommunicationMain phase.
///

/// Provider Web Part
/// The args passed by the Provider
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
{
if(cellReadyArgs.Cell != null)
{
pageUrl = cellReadyArgs.Cell.ToString();
}
}

///


/// Render this Web Part control to the output parameter specified.
///

/// The HTML writer to write out to
protected override void Render(HtmlTextWriter output)
{
//The following objects are used to created the iframe in the web part and return the contents of the link to the IFRAME
StringBuilder buffer = new StringBuilder(10240);
StringWriter InnerWriter = new StringWriter(buffer);
HtmlTextWriter BufferWriter = new HtmlTextWriter(InnerWriter);

try
{
//Call method to write HTML to buffer
WriteWebPartContent(BufferWriter);
//Write HTML back to browser
output.Write(buffer);
}
catch (System.Exception Ex)
{
}
}

///


/// This method generates the HTML to display the IFRAME and the page content inside it
///

/// The HTMLTextWriter to send the output to
protected void WriteWebPartContent(HtmlTextWriter output)
{
try
{
string frame = "outputIFrame_" + base.Qualifier;

output.AddStyleAttribute("display", "inline");
output.AddAttribute(HtmlTextWriterAttribute.Id, frame);
output.AddAttribute(HtmlTextWriterAttribute.Name, frame);
output.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
output.AddStyleAttribute(HtmlTextWriterStyle.Height, "100%");
output.AddAttribute("frameBorder", "0");
output.AddAttribute(HtmlTextWriterAttribute.Src, pageUrl);
output.RenderBeginTag(HtmlTextWriterTag.Iframe);
output.RenderEndTag();

//If the web part is configured to show the page url then display it
if (showPageUrl == true)
{
output.Write( "
This web part is displaying content from the following URL: " + SPEncode.HtmlEncode(pageUrl) + "
");

output.Write( "
EASTER EGG! Ski lifts open in 97 days from the date I reposted this article!
");


}
}
catch (System.Exception Ex)
{
}
}
}
}

Related Posts

17-Time Microsoft MVP

Microsoft Most Valuable Professional

5-Time Microsoft Fast Track Recognized Solution Architect (FTRSA)

Microsoft FastTrack Recognized Solution Architect
© 2026 Todd Baginski's Blog. All Rights Reserved.