in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Todd Baginski's SharePoint 2003 and MOSS 2007 Blog

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

Comments

 

Guruprasad Karnik said:

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.

August 17, 2007 2:19 AM
 

15 Links Today (2007-08-18) said:

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

August 18, 2007 10:23 AM
 

presack said:

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

August 21, 2007 7:22 PM
 

Miles said:

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;

}

}

February 12, 2008 7:01 PM
 

Sundar Narasiman said:

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

February 13, 2008 2:46 AM
 

Quin said:

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.

April 8, 2008 3:03 PM
 

scott said:

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.

April 15, 2008 12:42 PM
 

MDeveloper said:

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 .

April 29, 2008 9:26 AM
 

MDeveloper said:

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

Thanks,

MDeveloper

May 8, 2008 1:31 PM
 

Josep said:

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

June 13, 2008 5:28 AM
 

sopa said:

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

July 3, 2008 9:50 AM
 

Suman said:

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

Thank you SharePoint Blog....

August 19, 2008 6:35 AM
 

gregsoc said:

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.

August 19, 2008 12:25 PM
 

Srinath said:

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

October 27, 2008 2:22 AM
 

Gaurav said:

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

October 30, 2008 9:52 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

About tbaginski

Todd began working with WSS and SPS in the beta stages of the products, in 2003. He has architected and implemented intranets, extranets, wireless deployments, and several custom applications with the WSS and SPS platforms. Todd created the first SharePoint advanced development training curriculum in the training industry for Microsoft SharePoint Portal Server 2003 and taught his first class in December of 2003. Todd also created the first advanced development training curriculum for Microsoft Office SharePoint Server (MOSS) 2007 and taught parts of this curriculum while MOSS was still in the Beta 1 Technical Refresh stage! Two versions in a row, Todd has broken ground in the training industry by being the first to offer advanced development training for SharePoint! Todd speaks at many SharePoint conferences including SharePoint Connections, the MS SharePoint Conference, and the upcoming TechEd 2008 Developers Conference. He has also spoken at Microsoft events at the Microsoft offices in both Denver, Colorado and Minneapolis, Minnesota. He has also presented several sessions at the Rocky Mountain SharePoint User's Group. He enjoys spending time with his wife and their dogs, playing and coaching lacrosse, playing hockey, riding his bike in the Rocky Mountains, and skiing every chance he gets.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts