Responsive Ads Here

Tuesday, September 4, 2012

Office Web Apps SharePoint 2010 - IFrame


Issue:
1. In a project I'm working on we use Office Web Apps in SP2010 to preview selected documents in the browser.
2. We've created a very simple web part that renders an I-Frame with the URL set to one of the Office Web Apps urls depending on the document extension. Now the are not renderring in IFrame.

Reason:
Unfortunately the X-Frame header, that is added by the Office Web Apps service, prevents Internet Explorer to render the documents in an I-Frame!

Solution:
To solve this we've created a very simple HttpModule that checks for the header and changes the value from "DENY" to "SAMEORIGIN". This post simply shows the code for such a module that enables previewing of documents with Office Web Apps inside an I-Frame.

Implement the Module

http://support.microsoft.com/kb/307996
  1. Create a new Visual Studio .NET C# Class Library project named MyModule.
  2. Set a reference to the System.Web.dll assembly.
  3. Add the following directive to the class:
    using System.Web;     
  4. Rename the class SyncModule.cs, and then change the class definition to reflect this.
  5. Implement the IHttpModule interface. Your class definition should appear as follows:
    public class OfficeWebAppIntModule : IHttpModule
  6. Write the following code in the interface which changes the value of header(
    "X-FRAME-OPTIONS") from "DENY" to "SAMEORIGIN" in the Init event.
 
public class OfficeWebAppIntModule : IHttpModule
    {
        private const string XFrameOptionsHeaderName = "X-FRAME-OPTIONS";
        LogMessage errorLogger = new LogMessage();
    /// <summary>
    /// Initializes a new instance of the <see cref="XFrameOptionsModule"/> class.
    /// </summary>
        public OfficeWebAppIntModule()
        {
        }
 
        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
        }
 
        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {            
            context.PreSendRequestHeaders += new EventHandler(ChangeXFrameOptionsHeaderToSameOrigin);
            //context.PreSendRequestHeaders += ChangeXFrameOptionsHeaderToSameOrigin;
        }
 
        /// <summary>
        /// Changes the X-Frame-Options "DENY" header to "SAMEORIGIN".
        /// </summary>
        /// <param name="sender">The HttpApplication that triggers the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ChangeXFrameOptionsHeaderToSameOrigin(object sender, EventArgs e)
        {
            
                //throw new ApplicationException("Testing HTTP Module");
                HttpApplication application = (HttpApplication)sender;
                HttpResponse response = application.Response;
                
                string headerValue = response.Headers[XFrameOptionsHeaderName];
                
                if (headerValue != null && headerValue.Equals("DENY", StringComparison.OrdinalIgnoreCase))
                {
                    response.Headers[XFrameOptionsHeaderName] = "SAMEORIGIN";
                }            
            
        }
 
    }
 
 7. Compile the Project.
 

Deploy the Module

Register the dll into GAC
 

Configure the System

  1. In the C:\Inetpub\Wwwroot\Module\ directory, create a new file named Web.config.
  2. Paste the following text in Web.config:
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
     
      <add name="OfficeWebAppIntegration"type="OfficeWebAppIntegration.OfficeWebAppIntModule, OfficeWebAppIntegration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bb6b20dfb6d61a1" preCondition="managedHandler" />

    </modules>
</system.webServer>

No comments:

Post a Comment