SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Connected Page Viewer Web Part

This blog has moved.  Click here to open this post on my new blog.

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

This blog has moved.  Click here to comment on this post on my new blog.


Posted 08-16-2007 2:11 PM by tbaginski

Comments

Guruprasad Karnik wrote re: Connected Page Viewer Web Part
on 08-17-2007 2:19 AM

Now with .NetFrameWork 2.0 developing connectable web parts has become much simpler. I need not override 8-9 methods as it was in WSS 2.0. I just need to implement the IWebPart field interface and override 3-4 methods each containing 2-3 lines. Moreover in WSS 2.0 if I had to develop a CellProvider and a FilterProvider it was a nightmare where as in this case its much simpler.

15 Links Today (2007-08-18) wrote 15 Links Today (2007-08-18)
on 08-18-2007 10:23 AM

Pingback from  15 Links Today (2007-08-18)

presack wrote re: Connected Page Viewer Web Part
on 08-21-2007 7:22 PM

This looks like a great solution- nice work!  

Being new to SharePoint, though, I wonder if anybody could be so kind as to provide a little bit more guidance on deploying the web part.  I realize that's fairly well-covered ground, but it seems like different downloadable web parts have different methods of installation.

So-

I copy your code to a text file and save it as????

Create a dwp file?  (How???)

Something about strong name encrpytion code?  STSadmin?

I hate asking so many questions, but I want to have a good idea of exactly what I am asking our administrator to do before I ask them to do it.

Thanks,

presack

Miles wrote re: Connected Page Viewer Web Part
on 02-12-2008 7:01 PM

I added this code to make it work with a WSS 3.0 Links List:

public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)

{

if(cellReadyArgs.Cell != null)

{

   //modified to work with wss3.0 link list

   string sURL = cellReadyArgs.Cell.ToString();

   if (sURL.IndexOf(",") > 0)

   {

       sURL = sURL.Substring(0,sURL.IndexOf(","));

   }//

   pageUrl = sURL;

}

}

Sundar Narasiman wrote Connected Page Viewer Web Part
on 02-13-2008 2:46 AM

Scenario:- There is a Link web part and a Page viewer web part on the web part page. The requirement

Quin wrote re: Connected Page Viewer Web Part
on 04-08-2008 3:03 PM

Is there an update to this?  It doesn't build with VS2008.  There are 19 messages talking about obsolete methods, that is, most of the WebPart and WebPartPages methods.

scott wrote re: Connected Page Viewer Web Part
on 04-15-2008 12:42 PM

I'm a total newbie at this, but trying. I copied and pasted the raw code into a page, named it, and tried to open it with my MOSS site. Received, "...error occurred during the processing of /filename.aspx. The server tag is not well formed."

Is this a familiar error? What do I need to do?

Thanks.

MDeveloper wrote re: Connected Page Viewer Web Part
on 04-29-2008 9:26 AM

How to implement the IWebPart field interface and override 3-4 required methods for that?

can you please send the code and explain?

Thanks a lot .

MDeveloper wrote re: Connected Page Viewer Web Part
on 05-08-2008 1:31 PM

Can anyone post the code for Connectable Page Viewer Webpart using IRowConsumer?

Thanks,

MDeveloper

Josep wrote re: Connected Page Viewer Web Part
on 06-13-2008 5:28 AM

have you got the dwp file??? I tried the code for MOSS 2007 and it doesn't works

sopa wrote re: Connected Page Viewer Web Part
on 07-03-2008 9:50 AM

Hi,

is there any posibility for you to rewrite the code NOT using ICellProvider and ICellConsumer since it is obsolete?

Can one use System.Web.UI.WebControls.WebParts.WebPart in a sharepoint site, instead of using sharepoin.webparts.webpart?

Thanks in before hand.

Regarde sopa

Suman wrote re: Connected Page Viewer Web Part
on 08-19-2008 6:35 AM

Really very helpful i have used this webpart with Links Webpart in MOSS 2007 working fine

Thank you SharePoint Blog....

gregsoc wrote re: Connected Page Viewer Web Part
on 08-19-2008 12:25 PM

Lots of unanswered questions here that I would also ask...

Can anyone post the code for Connectable Page Viewer Webpart using IRowConsumer?

Have you got the dwp file???  

because as great as this webpart sounds... I can't get it to work.

Srinath wrote re: Connected Page Viewer Web Part
on 10-27-2008 2:22 AM

Suman, how you are able to link with links webpart. I am unable to do that. Please provide me with the steps.

Gaurav wrote re: Connected Page Viewer Web Part
on 10-30-2008 9:52 PM

How to link the webpart with the linklist. Will there be any property?

Big wrote re: Connected Page Viewer Web Part
on 12-03-2008 2:48 PM

Has anyone gotten this web part to work with MOSS 2007?

I copied the code from this article; created a new Web Part in VS 2008; Built the code without error after resolving ambiguous class names; Tried to deploy the webpart and recieved the following error:

Incompatible Web Part markup detected. Use *.dwp Web Part XML instead of *.webpart Web Part XML.

I would love to use this web part but it seems it doesn't work with MOSS 07.

Any help would be greatly appreciated!

Thanks,

Big

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.