Skip to main content Skip to footer

Using ActiveReports with LightSwitch Application

Recently we had few developers looking to use ActiveReports with LightSwitch Application. Based on their request and looking at the probability of increasing use of ActiveReports with LightSwitch Application, I am writing this blog to show how ActiveReports can be intergrated easily with LightSwitch. At this point of time, ActiveReports does not have any assembly which can show its ActiveReports Viewer control directly in the LightSwitch. Neither the Silverlight Viewer assembly is supported inside LightSwitch. So our solution is to create a Silverlight UserControl which hosts the Silverlight Viewer and then integrate this usercontrol inside the LightSwitch Application Screen as CustomControl.

Creating a Silverlight UserControl With ActiveReports

First of all create a Silverlight Project with a UserControl page. Drag drop the Silverlight Viewer control on this UserControl XAML designer. Rebuild the Application.

Adding Viewer Control a LightSwitch Application

Next step involves integrating the above UserControl inside LightSwitch Application. Follow the given steps to create a lightSwitch application.

  1. Create a new LightSwitch project.
  2. Add a new Editable screen.
  3. Switch to File View in your Solution Explorer and add reference to Silverlight Viewer assembly 'GrapeCity.ActiveReports.Viewer.Silverlight.v7' to the Client Section.
  4. Open the newly added Screen and add the Silverlight UserControl as new Custom Control.

AddCustomControl

Adding and Accessing Reports in Viewer Control

Coming to this important section of the implementation, we need to add the Report object and this has to be done in the Server Model of the LightSwitch app. Switch to File View from the Solution Explorer and create a new Report or add an existing report object to your LightSwitch Application. ReportService To load this ActiveReports in SilverlightViewer control for display, we have to stream the report in RDF format to our Viewer control. However, our report is in Server domain for LightSwitch application whereas the viewer is hosted in the Client. To stream the RDF format, we have to add a web page which will redirect the Stream data to the client Add a web page 'Report.aspx' to the Server project and add the following code to the Page_Load event.



protected void Page_Load(object sender, EventArgs e)  
{  
   //Runs CodeReport in the Server->generates its rdf and postsback to the client  
   using (MainReport report = new MainReport())  
   {  
      report.Run();  
      using (MemoryStream ms = new MemoryStream())  
      {  
        report.Document.Save(ms, GrapeCity.ActiveReports.Document.Section.RdfFormat.ARNet);  
        ms.Position = 0;  
        Response.BinaryWrite(ms.ToArray());  
      }  
   }  
}  

Apart from this, to access the web page between two project i.e. to access the 'Report.aspx' at Client project, we have to grant special Cross Domain and Client Access permissions. Add the following policy files to the Server project.

  • crossdomainpolicy.xml
  • clientaccesspolicy.xml

These files are available with the attached sample.

Displaying Reports in Silverlight UserControl

To display the report in the viewer, we have to get access to this control in MainScreen.cs. Apart from this, we will also display a Busy indicator to show the loading of reports. Add the following code in MainScreen.cs to get access to the controls.


System.Windows.Controls.BusyIndicator busyindicator;  

partial void MainScreen_Created()  
{  
  // Write your code here.  
  var bproxy = this.FindControl("ScreenContent1");  
  bproxy.ControlAvailable += bproxy_ControlAvailable;  

  var proxy = this.FindControl("ScreenContent");  
  proxy.ControlAvailable += proxy_ControlAvailable;  
}  

void bproxy_ControlAvailable(object sender, ControlAvailableEventArgs e)  
{  
  busyindicator = e.Control as System.Windows.Controls.BusyIndicator;  
}  

In the above code snippet, we can observe the events 'ControlAvailable' attached to the controls. This event is fired when the control is loaded in the MainScreen and this is the point where we start the actual processing of displaying the reports. These final code blocks mentioned below show how the stream data is retrieved for the Report from Server project and displayed in the Viewer control.


GrapeCity.ActiveReports.Viewer Viewer;  

void proxy_ControlAvailable(object sender, ControlAvailableEventArgs e)  
{  

   Viewer = ((System.Windows.Controls.Grid)(((e.Control as SL\_AR\_Viewer.ARViewer).Content))).Children[0] as GrapeCity.ActiveReports.Viewer;  
   Uri codeuri = new Uri("../Report.aspx",UriKind.Relative);  
   WebClient codereport = new WebClient();  
   busyindicator.IsBusy = true;  
   codereport.OpenReadCompleted += new OpenReadCompletedEventHandler(codereport_OpenReadCompleted);  
   codereport.OpenReadAsync(codeuri);  
}  

private void codereport_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)  
{  
   if (e.Result != null)  
   {  
      Stream rdf = (Stream)e.Result;  
      LoadReportFromStream(rdf);  
      busyindicator.IsBusy = false;  
   }  
}  

private void LoadReportFromStream(Stream stream)  
{  
   //Loads the RDF stream and displays in the Silverlight Viewer  
   GrapeCity.Viewer.Common.StreamDocumentLoader rdfdocument = new GrapeCity.Viewer.Common.StreamDocumentLoader(stream, GrapeCity.Viewer.Common.DocumentFormat.Rdf);  
   Viewer.LoadDocument(rdfdocument);  
}  

LSreportOutput Download the attached sample application for complete reference. Sample

MESCIUS inc.

comments powered by Disqus