Archive

Archive for April, 2006

Programmatically resizing IFRAME height

April 30th, 2006 No comments

Have you ever tried to use “Page Viewer Webpart” in sharepoint ? There is a big problem setting the height for this webpart when the page, that is loaded in its iframe, varies in height. In some cases scrollbars appear even if the height is set in webpart property. In respond to a client request, I have recently developed a WSS template that contains a web part which iterates through a pre-defined/pre-populated document library, looking  for default.aspx or default.html and renders that page in an IFRAME. I had to dynamically resize the document library in run time according to the height of the page it points to. My solution contains a java script file named “iFrameResizer.js” that is set to be “Content” of my web part and a little bit of code in my web part to link to this script file to my web part.


 


//—————————————————————–


//iFrameResizer.js


//—————————————————————–


/* This function is used to adjust the iframe height to its content */
function iResizer(iframeID,iframeName)
{


     moz=document.getElementById&&!document.all
     mozHeightOffset=20
    document.getElementById(iframeID).width=”100%”
    document.getElementById(iframeID).height=window.frames[iframeName].document.body.scrollHeight+(moz?mozHeightOffset:0)
}


 



Then in the constructor of  web part I added an event handler to the PreRender event that loads “iFrameResizer.js” onto the page on prerendering  :


 


using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;


namespace DevHorizon.Sharepoint.iFrameWP
{
 ///
 /// Description for Frame.
 ///
 [DefaultProperty(“Url”),
  ToolboxData(“<{0}:Frame runat=server>”),
  XmlRoot(Namespace=”DevHorizon.Sharepoint.iFrameWP”)]
 public class Frame : WebPart
 {
  private const string defaultUrl = “shared%20documents/default.htm”;
  private string url = defaultUrl;


  // Used for the linked script file
  private const string iFrameResizerFileName = “iFrameResizer.js”;
  private const string iFrameResizerIncludeScriptKey = “iFrameResizerIncludeScript”;
  private const string IncludeScriptFormat =@” language=””{0}”” src=””{1}{2}””>”;



 
  [Browsable(true),Category(“Miscellaneous”),DefaultValue(defaultUrl),WebPartStorage(Storage.Personal),FriendlyName(“Url”),Description(“URL Property”)]
  public string Url
  {
     get
     {
       return url;
      }


   set
     {
      url = value;
     }
  }


 


  // Contructor
  public Frame()
  {
      this.PreRender += new EventHandler(WebPart_Frame_PreRender);
  }


  //Function which will register the linked file script


 


  private void WebPart_Frame_PreRender(object sender , System.EventArgs e )
  {
       string location = null;
        // Make sure that the script was not already added to the page.
        if (!Page.IsClientScriptBlockRegistered(iFrameResizerIncludeScriptKey))
          {
           location = this.ClassResourcePath + “/”;
          // Create the client script block.
          string includeScript = String.Format(IncludeScriptFormat, “javascript”, location, iFrameResizerFileName);
          Page.RegisterClientScriptBlock(iFrameResizerIncludeScriptKey, includeScript);
         }


  }


 


  protected override void CreateChildControls()
  {
   Literal lit = new Literal();
   Guid gid=Guid.NewGuid();
   string iframeID=”ExxoniFrameWPId” + gid.ToString();
   string iframeName= “ExxoniFrameWPName” + gid.ToString();
   lit.Text = “”;


   this.Controls.Add(lit);


  }
 
  protected override void RenderWebPart(HtmlTextWriter output)
  {
      try
       {
          RenderChildren(output);
        }
     //display diagnostic error information if exception occurs
     catch(Exception ex)
      {
        output.Write(”


“);
        output.Write(“Exception Type: ” + ex.GetType().ToString() + “
“);
        output.Write(“Exception Message: ” + ex.Message + “


” );
        output.Write(“Stack Trace:” + ex.StackTrace.Replace(”
“, “
“));
        output.Write(“
“);
   }


  }
 }
}


 


There is one thing to remember:


I assume that iFrameResizer.js has been already added to the solution as “content” and is deployed to wpresources (or _wpresources if web part is installed in GAC). You have to add the following code to Manifest.xml

Categories: Uncategorized Tags: