Introduction

I was facing a problem to load pdf generated document in a popup window. The problem was user saves some information from a custom front end to SharePoint List and on the list event handler there was a code written to generate a SQL report and export it into pdf document all through code and save the pdf document to another SharePoint library.

Now the actual problem is when user saves the information, immediately user wanted to see the pdf document generated in a popup window. Now if I code everything (document generation code) on click of Save button then it takes a lot of time and frustrate user for waiting for a long to save the data.  So to follow a disconnected approach, on click of save we are just saving the data in SharePoint list and on the list there is an asynchronous event handler which generates the document when any item is added in the list.

Now you might have understood the background, here is what I am going to present the code which actually loads the pdf document in a popup window. I call it PDFLoader. It is a visual web part in SharePoint. 

Example

Visual Web Part: HTML Part

<div>
    <div id="documentLoadingMessage" >
    <b> Please wait while system is generating the document......   </b>
    </div>
     
    <div style="text-align: center; vertical-align:middle;padding-top:50px">
     <img src="../../../_layouts/myImages/images/progressbar.gif"  alt="Processing..." />
     </div>
</div>

Visual Web Part: Code Behind

using System;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HRCompetencyGuide.Model;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
using System.IO;
using Microsoft.Reporting.WebForms;
using System.Data;
 
namespace mynamespace
{
    public partial class PDFLoader : UserControl
    {
        //Default value is 5 second; page refresh time interval is used to reload the page if document has been generated from the event handler
        private const string PGAE_REFRESH_TIME_INTERVAL = "5";
 
        protected void Page_Load(object sender, EventArgs e)
        {           
            try
            {
                if (!loadDocument())
                {
                    //If document is not loaded (i.e. not generated by event receiver) then try to reload it after the refresh time interval, so waiting for some time for event handler to execute completely
                    Response.AppendHeader("Refresh", PGAE_REFRESH_TIME_INTERVAL);                  
                }
               
            }
            catch (Exception ex)
            {
              //handle exception
            }
        }
 
        private bool loadDocument()
        {
            bool isDocumentLoaded = false;
 
// code to load the pdf document if it is generated from the Event handler
// code will first check in the SharePoint library if isDocumentLoaded  flag is True, and accordingly return, if it is true then load the data and return, if not then just return the flag, according to this flag the auto refresh meta property is set in the header of the page
            
return isDocumentLoaded;
         
 
    }
}

References