Which WSS 3.0 / MOSS 2007 site Template is right for me?
June 29, 2007
Creating a Custom Document Library Feature in WSS V3 / MOSS 2007
August 16, 2007

Connected Page Viewer Web Part

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
{
/// <summary>
/// This Web Part displays the contents of a URL that is passed in from a provider web part.
/// </summary>
[DefaultProperty(“Text”),
ToolboxData(“<{0}:ConsumerPageViewer runat=server></{0}:ConsumerPageViewer>”),
XmlRoot(Namespace=”Custom.SharePoint.WebParts”)]
public class ConsumerPageViewer : WebPart , ICellConsumer
{
#region Properties

/// <summary>
/// The URL to display in the IFRAME
/// </summary>
private string pageUrl = string.Empty;
/// <summary>
/// The property that exposes the pageUrl variable
/// </summary>
[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;}
}

/// <summary>
/// Variable to toggle if the URL of the content in the web part is displayed.
/// </summary>
private bool showPageUrl = false;
/// <summary>
/// The property that exposes the showPageUrl variable
/// </summary>
[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”

/// <summary>
/// Register interfaces.
/// </summary>
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)
{
}
}

/// <summary>
/// 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.
/// </summary>
public override ConnectionRunAt CanRunAt()
{
return ConnectionRunAt.Server;
}

/// <summary>
/// Notification to the Web Part that it has been connected
/// </summary>
/// <param name=”interfaceName”>Unique name of the interface that is being connected</param>
/// <param name=”connectedPart”>Reference to the other Web Part that is being connected to</param>
/// <param name=”connectedInterfaceName”>Unique name of the interface on the other Web Part</param>
/// <param name=”runAt”>Where the interface should execute</param>
public override void PartCommunicationConnect(string interfaceName,
WebPart connectedPart,
string connectedInterfaceName,
ConnectionRunAt runAt)
{
EnsureChildControls();
if (interfaceName == “CellConsumer_WPQ_”)
{
cellConnectedCount++;
}
}

/// <summary>
/// In this method, a part should fire any connection init events.
/// </summary>
public override void PartCommunicationInit()
{
if(cellConnectedCount > 0)
{
if (CellConsumerInit != null)
{
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
CellConsumerInit(this, cellConsumerInitArgs);
}
}
}

/// <summary>
/// This method is called by the Authoring environment for all
/// the initial data required for creating a connection.
/// </summary>
/// <param name=”interfacename”>Name of interface that the framework is
/// requesting information on</param>
/// <returns>An EventArgs object such as CellProviderInitArgs</returns>
public override InitEventArgs GetInitEventArgs(string interfaceName)
{
if (interfaceName == “CellConsumer_WPQ_”)
{
EnsureChildControls();
CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();
cellConsumerInitArgs.FieldName = cellName;
return(cellConsumerInitArgs);
}
else
{
return(null);
}
}

/// <summary>
/// The CellProviderInit event handler
/// The Provider part will fire this event during its PartCommunicationInit phase.
/// </summary>
/// <param name=”sender”>Provider Web Part</param>
/// <param name=”cellProviderInitArgs”>The args passed by the Provider</param>
public void CellProviderInit(object sender, CellProviderInitEventArgs cellProviderInitArgs)
{
// This is where the Consumer part can identify what type of “Cell” the Provider
// will be sending.
}

/// <summary>
/// The CellReady event handler
/// The Provider part will fire this event during its PartCommunicationMain phase.
/// </summary>
/// <param name=”sender”>Provider Web Part</param>
/// <param name=”cellReadyArgs”>The args passed by the Provider</param>
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
{
if(cellReadyArgs.Cell != null)
{
pageUrl = cellReadyArgs.Cell.ToString();
}
}

/// <summary>
/// Render this Web Part control to the output parameter specified.
/// </summary>
/// <param name=”output”> The HTML writer to write out to </param>
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)
{
}
}

/// <summary>
/// This method generates the HTML to display the IFRAME and the page content inside it
/// </summary>
/// <param name=”output”>The HTMLTextWriter to send the output to</param>
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( “<BR>This web part is displaying content from the following URL: ” + SPEncode.HtmlEncode(pageUrl) + “<BR>”);

output.Write( “<BR>EASTER EGG! Ski lifts open in 97 days from the date I reposted this article!<BR>”);

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