How To Display Office Documents When Office is Unavailable

Document Toolkit for LightSwitch support Office documents such as Word, Excel and PowerPoint documents. Document Toolkit does not natively render Office documents itself, but instead relies on Office Save As XPS functionality. Whenever an Office document is rendered, it is converted to XPS first (what is XPS?).

Office support works when the following two conditions are met:
  1. Microsoft Office 2007 or 2010 is installed. As in if you want to display a Word document, you need to have Microsoft Word installed. PowerPoint presentations require PowerPoint, etc.
  2. The LightSwitch application runs with elevated permissions, meaning that the application must be deployed as desktop application.

Note: Supported Microsoft Office versions include Office 2007 (with the Save As XPS add-in) and Microsoft Office 2010. Older Office versions are not supported.

These requirements mean that Document Toolkit for LightSwitch is unable to render Office documents when Office is not installed, or when the application is deployed as web application. In these situations Document Toolkit displays a notification message:


Not all is lost. Despite these limitations it is still possible to create a LightSwitch application that is able to render Office documents even when the above requirements are not met. The solution is found in converting an Office document the moment it is added to the database and storing the converted XPS document as well. This still requires the user who is adding or updating documents to have Office installed, but for users who only read the documents, Office availability is not needed.

An extra benefit is that document conversion is only required when adding or updating a document. When reading documents there is no conversion needed, thus reading performance is improved significantly.

This step-by-step tutorial demonstrates an approach on how to enable Office support even when Office is not available.

1) Start Visual Studio LightSwitch and open the LightSwitch application that was created in the tutorial How To Create And Use A Table of Documents.

2) Open the Documents table and add a non-required property FileXps of type Binary and a computed property FileForDisplay also of type Binary.


The File property contains the original uploaded file. The FileXps property stores the XPS conversion result and the FileForDisplay property determines whether the original File or the FileXps should be displayed by Document Toolkit for LightSwitch. When an XPS or PDF document is uploaded, there is no need to convert the document to XPS since Document Toolkit natively supports XPS and PDF.

3) Edit the compute method of the FileForDisplay property and enter the following code:

partial void FileDisplay_Compute(ref byte[] result)
  if (this.FileXps != null) {
    // there is an XPS conversion result, display it
    result = this.FileXps;
  else {
    // otherwise display the original file
    result = this.File;

Private Sub FileForDisplay_Compute(ByRef result As Byte())
  If Not Me.FileXps Is Nothing Then
    ' there is an XPS conversion result, display it
    result = Me.FileXps
    ' otherwise display the original file
    result = Me.File
  End If
End Sub

4) We need to associate the DocumentViewer control to the new FileForDisplay property. Open the DocumentDetail screen previously created in the tutorial How To Create And Use A Table of Documents. Add the File For Display property and select the Document Viewer Control for display.


5) Remove the File property from the details screen so that the screen looks like this;


6) Open the SearchDocuments screen and navigate to the Execute code of the Add button. The current code opens a file dialog and stores the selected file as binary in the File property. We need to extend it and add code to convert a document to XPS when needed and store the XPS result in the FileXps property.

Modify the Execute method as demonstrated below;

partial void gridAddNew_Execute()
  var file = SelectFile();
  if (file != null) {
    using (var reader = DocumentReader.Create(file)) {
      var document = this.Documents.AddNew();
      document.Name = file.Name;
      document.File = reader.ToBinary();

      if (reader.RequiresConversionToXps) {
        // conversion needed, get the XPS binary
        document.FileXps = reader.ToXpsBinary();

Private Sub gridAddNew_Execute()
  Dim file = SelectFile()
  If Not file Is Nothing Then
    Dim reader = DocumentReader.Create(file)
    Using reader
      Dim document = Me.Documents.AddNew
      document.Name = file.Name
      document.File = reader.ToBinary

      If reader.RequiresConversionToXps Then
        ' conversion needed, get the XPS binary
        document.FileXps = reader.ToXpsBinary
      End If
    End Using
  End If
End Sub

7) And finally we want to disable the Add button when conversion to XPS is not available. This happens when the application is deployed as web application.

Modify the CanExecute method:

partial void gridAddNew_CanExecute(ref bool result)
  // only allow adding documents if conversion is enabled
  result = DocumentReader.IsConversionEnabled;

Private Sub gridAddNew_CanExecute(ByRef result As Boolean)
  ' only allow adding documents if conversion is enabled
  result = DocumentReader.IsConversionEnabled
End Sub

Done. Whenever a document is added to the database that requires Office for conversion, the conversion is executed the moment the document is uploaded and the XPS conversion result is also stored. When a document is requested for viewing, the XPS result is shown and no conversion is required. This allows Office document display in web applications or on machines that do not have Office installed.

Next step: Learn how to add upload and download functionality in How To Download and Update Documents.

Last edited Sep 26, 2011 at 8:56 PM by kozw, version 16


jmanleywi Nov 9, 2011 at 8:38 PM 
If the user does not have office installed will the Office Viewers work?