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 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)
{
}
}
}
}


Hi, a question for anyone who has implemented this web part. Will it display the contents of a mapped drive? I would like to be able to display the contents of a mapped drive ie (\serverfolder) in the CPVWP. So I have a list of mapped drives and I would like the user to be able to select an opttion from the list which will then display the contents of that folder in the CPCWP.
Thanks
Gary
Hi Gary,
I never tried this out myself so I’m not sure. A quick way to test would be to make an HTML page with an IFRAME and set the source of the IFRAME to your mapped drive. Perhaps a UNC path will work.
Todd
Got this to work in VS 2008 just fine – also as ASP.NET 3.5 web part simply using the following code:
protected override void Render(HtmlTextWriter writer)
{
Random rand = new Random();
string frame = "outputFrame_" + rand.Next(1000).ToString();
writer.AddStyleAttribute("display", "inline");
writer.AddAttribute(HtmlTextWriterAttribute.Id, frame);
writer.AddAttribute(HtmlTextWriterAttribute.Name, frame);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100%");
writer.AddAttribute("frameBorder", "0");
writer.AddAttribute(HtmlTextWriterAttribute.Src, URL);
writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
writer.RenderEndTag();
base.Render(writer);
}
Screen Size 12.1 Inch
Resolution Pixels 1280×800
Backlight Type LED
Aspect Ratio 16:9
Screen Surface Glossy
Data Connection 20-Pin
Application Laptop or Notebook
http://www.nbkit.com/...
AUO B121EW07 V.1 LCD screen LCD screen is the most important component of laptop.Maybe Other laptop parts or components could be replaced or repaired easily at low cost.but for LCD screen,in most cases it only can’t be compatible problem. For solve the following problem,broken lcd screen,bad pixels,white lines,color,shine screen.Replace A LCD screen is only solution.Sometime,you need to make sure anti-static steps prepared before remove old screens and check new lcd screen parameters match your original LCD screen. so sugguest buyers need buy it at professional laptop screen website.
The LCD screen review from NBkit.com
12.1" LCD Screen has been experienced great changes from year 2005 to now. XGA–SXGA+–WXGA–WXGA HD, 5 Kinds different resolution, 5 kinds diffent data connector, plus backlight and slim or not, really rich in changes. Honesly every DIY buyer should note this before any replace screen action. Nbkit.com do a leading steps , and list their LCD screen connector type ,pins quantity to make sure every buyer get a useful support. Let"s see the tables, size backlight pins resolution Thickness 12.1" 1 CCFL 20 1280×768 WXGA standard 12.1" 1 CCFL 20 1024×768 XGA standard 12.1" 1 CCFL 30 1024×768 XGA standard 12.1" LED 30small 1024×768 XGA Slim 12.1" 1 CCFL 20 1280×800 WXGA standard 12.1" LED 30small 1280×800 WXGA Slim 12.1" LED 40 1280×800 WXGA Slim 12.1" LED 40small 1280×800 WXGA Slim 12.1" LED 20 1280×800 WXGA standard 12.1" LED 30 1280×800 WXGA standard 12.1" LED 30small 1280×800 WXGA standard 12.1" LED 40 1280×800 WXGA standard 12.1" LED 40small 1280×800 WXGA standard 12.1" 1 CCFL 20 1400×1050 SXGA+ standard 12.1" 1 CCFL 30 1400x1050SXGA+ standard 12.1" LED 40small 1440×900 WXGA+ standard 12.1" LED 30 1366×768 WXGA HD standard 12.1" LED 40 1366×768 WXGA HD standard
AUO B133EW07 V.0 LCD screen
Screen Size 13.3 Inch
Resolution Pixels 1280×800
Backlight Type LED
Aspect Ratio 16:9
Screen Surface Glossy
Data Connection 30-Pin
Application Laptop or Notebook
AUO B133EW07 V.0 LCD screen LCD screen is the most important component of laptop.Maybe Other laptop parts or components could be replaced or repaired easily at low cost.but for LCD screen,in most cases it only can’t be compatible problem. For solve the following problem,broken lcd screen,bad pixels,white lines,color,shine screen.Replace A LCD screen is only solution.Sometime,you need to make sure anti-static steps prepared before remove old screens and check new lcd screen parameters match your original LCD screen. so sugguest buyers need buy it at professional laptop screen website.
The LCD screen review from NBkit.com
13.3 LCD screen is widely used in laptops. although it experienced many changes , from CCFL LED, data pin changes, resolution from 1280×800 to 1366×768, but this it still a firm part make up of standard laptops on market. Almost all laptop company has 13.3" laptops , like HP, DELL, Acer, Asus, etc. it is classic size in laptop world. Currently , the main trend of 13.3" LCD Screen is Slim, LED backlight , high brightness and HD resolution..