Last week we made two big announcements regarding our Reporting Suite. The first one is that it will fully support Visual Studio 2010 and the second one is that we are introducing Report Viewer controls for both WPF and Silverlight.
I know a lot of you have been waiting for the Silverlight Viewer so here is a step by step to get you started. Note: If you are not familiar with creating reports, you can quickly catch up by watching the XtraReports How-To Videos:
DocumentPreview
The reports are rendered using a new DocumentPreview (DevExpress.Xpf.Printing.DocumentPreview) from our printing library. The DocumentPreview knows nothing about the reports themselves. It is the job of DocumentPreview.Model (an IDocumentPreviewModel) to provide the necessary plumbing.
namespace DevExpress.Xpf.Printing {
public interface IDocumentPreviewModel : INotifyPropertyChanged {
int CurrentPageIndex { get; set; }
int PageCount { get; }
void CreateDocument();
...
}
}
An implementation of one that works via WCF Web Services can be found in DevExpress.Xpf.Printing.ReportPreviewModel. This is the one we will be using to query for our XtraReports from the Silverlight App.
IReportService
Under the hood, ReportPreviewModel will communicate using an IReportService service contract:
public interface IReportService {
IList<ReportInfo> GetAvailableReports();
...
DocumentId StartBuild(string reportTypeName);
void StopBuild(DocumentId documentId);
}
so we must publish it on our server. The fastest way to do this is to create a new Silverlight-enabled WCF Service and then inherit it from a build ReportService. For example I will call it XtraReportService.
[ServiceContract]
public interface IXtraReportService : XtraReports.Service.IReportService {
// TODO: Add Custom Service Operations
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class XtraReportService : XtraReports.Service.ReportService, IXtraReportService {
}
The WCF Service wizard will add the default config. settings for your new service. Change the contract to your IXtraReportService.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="XtraReportServiceBinding">
<binaryMessageEncoding/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<services>
<service name="DevExpress.Samples.ReportViewer.Web.ReportService">
<endpoint address="" binding="customBinding" bindingConfiguration="XtraReportServiceBinding" contract="[FULL NAMESPACE].IXtraReportService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
Registering Reports
Now that we have the Report Service ready we need to tell it which XtraReport reports are available. This can be done in the config. file using a special DevExpress.XtraReports section. For example I can register my CustomerListReport in Web.config.
<DevExpress.XtraReport>
<RegisteredReports>
<Report TypeName="[Full XtraReport Type name]" Description="Customer List" Name="CustomerListReport">
<Permissions>
<add Action="Allow" Users="*" />
</Permissions>
</Report>
</RegisteredReports>
</DevExpress.XtraReport>
or register it in code:
ReportServiceContainer.Accessor.RegisterReport(
new DevExpress.XtraReports.Service.Common.Access.RegisteredReportInfo() {
DisplayName = "Customer List",
Permissions = new IReportPermission[] {
new ReportPermission(System.Web.Configuration.AuthorizationRuleAction.Allow, "*")
},
ReportType = typeof(MyNamespace.XtraReport_CustomerList)
});
UI
To display the report, drop the DocumentPreviewControl on your Silverlight page.
xmlns:my="clr-namespace:DevExpress.Xpf.Printing;assembly=DevExpress.Xpf.Printing.v10.1"
<my:DocumentPreviewControl Name="documentPreviewControl1"/>
and then query for it somewhere like so:
private void button2_Click(object sender, RoutedEventArgs e) {
ReportPreviewModel previewModel = new ReportPreviewModel();
previewModel.ReportTypeName = "MyNamespace.XtraReport_CustomerList";
documentPreviewControl1.Model = previewModel;
previewModel.CreateDocument();
}
Cheers,
Azret