{"id":16,"date":"2007-08-16T20:11:00","date_gmt":"2007-08-17T02:11:00","guid":{"rendered":"http:\/\/localhost\/todd_wp\/connected-page-viewer-web-part\/"},"modified":"2013-07-01T14:07:04","modified_gmt":"2013-07-01T20:07:04","slug":"connected-page-viewer-web-part","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/connected-page-viewer-web-part\/","title":{"rendered":"Connected Page Viewer Web Part"},"content":{"rendered":"\n<p>This is a repost of an article on my blog that was lost during a server failure.\u00a0 Historically, this\u00a0has been one of the most popular\u00a0articles on my blog, so I dug up the code and put it back online.<\/p>\n<p>The code you will find below is for a Connected Page Viewer Web Part for WSS V2 and SPS 2003.\u00a0 The Connected Page Viewer Web Part consumes a URL from another Web Part that provides URLs.\u00a0 This Web Part WORKS GREAT in WSS V3 and MOSS 2007 as well.<\/p>\n<p>Enjoy!<\/p>\n<p>using System;<br \/>using System.Web.UI;<br \/>using System.Web.UI.WebControls;<br \/>using System.ComponentModel;<br \/>using System.Xml.Serialization;<br \/>using System.Security;<br \/>using System.Security.Permissions;<br \/>using Microsoft.SharePoint;<br \/>using Microsoft.SharePoint.Utilities;<br \/>using Microsoft.SharePoint.WebPartPages;<br \/>using Microsoft.SharePoint.WebPartPages.Communication;<br \/>using System.Text;<br \/>using System.IO;\n<\/p>\n<p>namespace Custom.SharePoint.WebParts<br \/>{<br \/>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ This Web Part displays the contents of a URL that is passed in from a provider web part.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>[DefaultProperty(&#8220;Text&#8221;),<br \/>ToolboxData(&#8220;&lt;{0}:ConsumerPageViewer runat=server&gt;&lt;\/{0}:ConsumerPageViewer&gt;&#8221;),<br \/>XmlRoot(Namespace=&#8221;Custom.SharePoint.WebParts&#8221;)]<br \/>public class ConsumerPageViewer : WebPart , ICellConsumer<br \/>{<br \/>#region Properties\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ The URL to display in the IFRAME<br \/>\/\/\/ &lt;\/summary&gt;<br \/>private string pageUrl = string.Empty;<br \/>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ The property that exposes the pageUrl variable<br \/>\/\/\/ &lt;\/summary&gt;<br \/>[Browsable(true), <br \/>Category(&#8220;Customization&#8221;), <br \/>DefaultValue(&#8220;&#8221;), <br \/>WebPartStorage(Storage.Personal), <br \/>FriendlyName(&#8220;Page Url&#8221;), <br \/>Description(&#8220;Default page URL such as <a href=\"http:\/\/www.sharepointexperts.com%22)]\/\" mce_href=\"http:\/\/www.sharepointexperts.com&quot;)]\">http:\/\/www.sharepointexperts.com&#8221;)]<\/a><br \/>public string PageUrl<br \/>{<br \/>get {return pageUrl;}<br \/>set {pageUrl = value;}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ Variable to toggle if the URL of the content in the web part is displayed.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>private bool showPageUrl = false;<br \/>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ The property that exposes the showPageUrl variable<br \/>\/\/\/ &lt;\/summary&gt;<br \/>[Browsable(true), <br \/>Category(&#8220;Customization&#8221;), <br \/>DefaultValue(false), <br \/>WebPartStorage(Storage.Shared), <br \/>FriendlyName(&#8220;Show Page Url&#8221;), <br \/>Description(&#8220;Specifies if the URL of the content in the web part is displayed.&#8221;)]<br \/>public bool ShowPageUrl<br \/>{<br \/>get {return showPageUrl;}<br \/>set {showPageUrl = value;}<br \/>}<br \/>#endregion<br \/>\/\/ Event required by ICellConsumer<br \/>public event CellConsumerInitEventHandler CellConsumerInit;\n<\/p>\n<p>\/\/ Keep a count of ICellConsumer connections.<br \/>private int cellConnectedCount = 0;\n<\/p>\n<p>\/\/ Cell information<br \/>private string cellName = &#8220;Cell Data&#8221;\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ Register interfaces.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public override void EnsureInterfaces()<br \/>{<br \/>try<br \/>{<br \/>\/\/ Register the ICellConsumer interface.<br \/>RegisterInterface(&#8220;CellConsumer_WPQ_&#8221;, <br \/>&#8220;ICellConsumer&#8221;, <br \/>WebPart.UnlimitedConnections, <br \/>ConnectionRunAt.Server, <br \/>this, <br \/>&#8220;CellConsumer_WPQ_&#8221;, <br \/>&#8220;Consumes a cell from&#8221;, <br \/>&#8220;Consumes a cell of data&#8221;); <br \/>} <br \/>catch(SecurityException)<br \/>{<br \/>}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ This method is called by the framework to determine whether a part <br \/>\/\/\/ can be run on the client or server based on the current<br \/>\/\/\/ configuration.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public override ConnectionRunAt CanRunAt()<br \/>{<br \/>return ConnectionRunAt.Server;<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ Notification to the Web Part that it has been connected<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;interfaceName&#8221;&gt;Unique name of the interface that is being connected&lt;\/param&gt;<br \/>\/\/\/ &lt;param name=&#8221;connectedPart&#8221;&gt;Reference to the other Web Part that is being connected to&lt;\/param&gt;<br \/>\/\/\/ &lt;param name=&#8221;connectedInterfaceName&#8221;&gt;Unique name of the interface on the other Web Part&lt;\/param&gt;<br \/>\/\/\/ &lt;param name=&#8221;runAt&#8221;&gt;Where the interface should execute&lt;\/param&gt;<br \/>public override void PartCommunicationConnect(string interfaceName,<br \/>WebPart connectedPart,<br \/>string connectedInterfaceName,<br \/>ConnectionRunAt runAt)<br \/>{<br \/>EnsureChildControls(); <br \/>if (interfaceName == &#8220;CellConsumer_WPQ_&#8221;)<br \/>{<br \/>cellConnectedCount++;<br \/>}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ In this method, a part should fire any connection init events.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public override void PartCommunicationInit()<br \/>{<br \/>if(cellConnectedCount &gt; 0)<br \/>{<br \/>if (CellConsumerInit != null)<br \/>{<br \/>CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs();<br \/>cellConsumerInitArgs.FieldName = cellName;<br \/>CellConsumerInit(this, cellConsumerInitArgs);<br \/>}<br \/>}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ This method is called by the Authoring environment for all<br \/>\/\/\/ the initial data required for creating a connection.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;interfacename&#8221;&gt;Name of interface that the framework is<br \/>\/\/\/ requesting information on&lt;\/param&gt;<br \/>\/\/\/ &lt;returns&gt;An EventArgs object such as CellProviderInitArgs&lt;\/returns&gt;<br \/>public override InitEventArgs GetInitEventArgs(string interfaceName)<br \/>{<br \/>if (interfaceName == &#8220;CellConsumer_WPQ_&#8221;)<br \/>{<br \/>EnsureChildControls();<br \/>CellConsumerInitEventArgs cellConsumerInitArgs = new CellConsumerInitEventArgs(); <br \/>cellConsumerInitArgs.FieldName = cellName;<br \/>return(cellConsumerInitArgs);<br \/>}<br \/>else<br \/>{<br \/>return(null);<br \/>}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ The CellProviderInit event handler<br \/>\/\/\/ The Provider part will fire this event during its PartCommunicationInit phase.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;sender&#8221;&gt;Provider Web Part&lt;\/param&gt;<br \/>\/\/\/ &lt;param name=&#8221;cellProviderInitArgs&#8221;&gt;The args passed by the Provider&lt;\/param&gt;<br \/>public void CellProviderInit(object sender, CellProviderInitEventArgs cellProviderInitArgs)<br \/>{<br \/>\/\/ This is where the Consumer part can identify what type of &#8220;Cell&#8221; the Provider<br \/>\/\/ will be sending.<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ The CellReady event handler<br \/>\/\/\/ The Provider part will fire this event during its PartCommunicationMain phase.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;sender&#8221;&gt;Provider Web Part&lt;\/param&gt;<br \/>\/\/\/ &lt;param name=&#8221;cellReadyArgs&#8221;&gt;The args passed by the Provider&lt;\/param&gt;<br \/>public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)<br \/>{<br \/>if(cellReadyArgs.Cell != null)<br \/>{<br \/>pageUrl = cellReadyArgs.Cell.ToString();<br \/>}<br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt; <br \/>\/\/\/ Render this Web Part control to the output parameter specified.<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;output&#8221;&gt; The HTML writer to write out to &lt;\/param&gt;<br \/>protected override void Render(HtmlTextWriter output)<br \/>{<br \/>\/\/The following objects are used to created the iframe in the web part and return the contents of the link to the IFRAME<br \/>StringBuilder buffer = new StringBuilder(10240);<br \/>StringWriter InnerWriter = new StringWriter(buffer);<br \/>HtmlTextWriter BufferWriter = new HtmlTextWriter(InnerWriter);\n<\/p>\n<p>try <br \/>{ <br \/>\/\/Call method to write HTML to buffer<br \/>WriteWebPartContent(BufferWriter);<br \/>\/\/Write HTML back to browser<br \/>output.Write(buffer); <br \/>}<br \/>catch (System.Exception Ex)<br \/>{ <br \/>} <br \/>}\n<\/p>\n<p>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ This method generates the HTML to display the IFRAME and the page content inside it<br \/>\/\/\/ &lt;\/summary&gt;<br \/>\/\/\/ &lt;param name=&#8221;output&#8221;&gt;The HTMLTextWriter to send the output to&lt;\/param&gt;<br \/>protected void WriteWebPartContent(HtmlTextWriter output)<br \/>{<br \/>try<br \/>{<br \/>string frame = &#8220;outputIFrame_&#8221; + base.Qualifier;\n<\/p>\n<p>output.AddStyleAttribute(&#8220;display&#8221;, &#8220;inline&#8221;);<br \/>output.AddAttribute(HtmlTextWriterAttribute.Id, frame);<br \/>output.AddAttribute(HtmlTextWriterAttribute.Name, frame);<br \/>output.AddStyleAttribute(HtmlTextWriterStyle.Width, &#8220;100%&#8221;);<br \/>output.AddStyleAttribute(HtmlTextWriterStyle.Height, &#8220;100%&#8221;);<br \/>output.AddAttribute(&#8220;frameBorder&#8221;, &#8220;0&#8221;);<br \/>output.AddAttribute(HtmlTextWriterAttribute.Src, pageUrl);<br \/>output.RenderBeginTag(HtmlTextWriterTag.Iframe);<br \/>output.RenderEndTag();\n<\/p>\n<p>\/\/If the web part is configured to show the page url then display it<br \/>if (showPageUrl == true)<br \/>{<br \/>output.Write( &#8220;&lt;BR&gt;This web part is displaying content from the following URL: &#8221; + SPEncode.HtmlEncode(pageUrl) + &#8220;&lt;BR&gt;&#8221;);<\/p>\n<p>output.Write( &#8220;&lt;BR&gt;EASTER EGG! Ski lifts open in 97 days from the date I reposted this article!&lt;BR&gt;&#8221;);<\/p>\n<p>}<br \/>}<br \/>catch (System.Exception Ex)<br \/>{<br \/>}<br \/>}<br \/>}<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a repost of an article on my blog that was lost during a server failure.\u00a0 Historically, this\u00a0has been one of the most popular\u00a0articles on<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[97,84],"tags":[10,5,7],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-sharepoint","category-sharepoint-2007","tag-custom-development","tag-helpful-code","tag-sharepoint"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":1,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":852,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions\/852"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}