This is a migrated thread and some comments may be shown as answers.

how do I load a trdx file in a web api project

2 Answers 442 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Glenn
Top achievements
Rank 1
Glenn asked on 08 May 2014, 04:10 PM
Greetings.

I'm not sure how to do this so asking it here. I have a Web API project that I've added the HTML5 ReportViewer.  I have looked at the examples included with Q3 2013 release but they reference a dll, not the trdx file. What I need to do is find the plumbing to:
1) load the trdx file
2) assign the dataset to the report
3) display the report

Simple eh?  But not if you can't find an example.  In the past I was able to do this on a windows form by:
<snippet>
 // reportInfo is a class that has all the report details, header, DataSet, etc
   XDocument doc = LoadXDocument(reportInfo); 

// open the report
 BufferedStream stream = new BufferedStream(new MemoryStream());
 stream.Write(Encoding.ASCII.GetBytes(doc.ToString()), 0, doc.ToString().Length);
 stream.Seek(0, SeekOrigin.Begin);
 StreamReader sr = new StreamReader(stream); 
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
 settings.IgnoreWhitespace = true; 

XmlDocument xmlDoc = GetXmlDocument(doc);
 Telerik.Reporting.Report report; 

using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(sr, settings))
 {
  Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer = new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();  
report = (Telerik.Reporting.Report)xmlSerializer.Deserialize(xmlReader);
 } 

Telerik.Reporting.TextBox tbHeader = (Telerik.Reporting.TextBox)report.Items["pageHeaderSection1"].Items["textBoxHeader"];
 tbHeader.Value = reportInfo.Title; 

if (reportInfo.CompanyLogo != "" && reportInfo.CompanyLogo != null)
 {
  Telerik.Reporting.PictureBox pictureBoxCompanyLogo = (Telerik.Reporting.PictureBox)report.Items["pageHeaderSection1"].Items["pictureboxCompanyLogo"];
  pictureBoxCompanyLogo.MimeType = reportInfo.CompanyLogoExt != "" ? "Image/" + reportInfo.CompanyLogoExt.ToUpper() : "";
  pictureBoxCompanyLogo.Value = Image.FromFile(reportInfo.CompanyLogo);
 } 

foreach (TableInfo tableinfo in reportInfo.Tables)
 {
  Telerik.Reporting.Table table = (Telerik.Reporting.Table)report.Items["detailSection1"].Items[tableinfo.ReportTableName];
  table.DataSource = tableinfo.DataSource;
 }
 
</snippet>    
    
 //routine that converts the XDocument (trdx) into XmlDocument
 private XmlDocument GetXmlDocument(XDocument document)
 {
  using (XmlReader xmlReader = document.CreateReader())
  {
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load(xmlReader);
   if (document.Declaration != null)
   {
    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration(document.Declaration.Version,
     document.Declaration.Encoding, document.Declaration.Standalone);
    xmlDoc.InsertBefore(dec, xmlDoc.FirstChild);
   }
   return xmlDoc;
  }
 }

oh if anyone wants to use that code, the one thing to point out is your DataTables need to have the same name as your Tables in the trdx else the Table cant assign the data.

I have to say that in the past all the responses the best I've ever gotten was a link to the documentation that only has partial code.  I just wish for once that someone would actually give me what I want, maybe a small web api project that loads a single report (trdx file) based on a datatable using the html5 viewer?
 
References:
http://www.telerik.com/help/reporting/telerik-reporting-rest-host-http-service-using-web-hosting.html
http://www.telerik.com/help/reporting/html5-report-viewer-embedding.html
many many others.....

Background Info
===================================
A lot of this should look familiar since it was basically cut and pasted from what I could find from your docs. 

WebApp (other sections not shown for simplicity)
----------------------------------------------
 Controllers
  ReportsController.cs
 Reports
  Sample1.trdx
 ReportViewer
  all files copied over from ReportViewer Folder and added to web app
 Views
  Reports
   Index.cshtml

ReportsController.cs
--------------------------------------------  
using System.Web;
using Telerik.Reporting.Cache.Interfaces;
using Telerik.Reporting.Services.Engine;
using Telerik.Reporting.Services.WebApi;namespace WebApp.Controllers
{
        public class ReportsController : ReportsControllerBase
    {
        protected override IReportResolver CreateReportResolver()
        {
            var reportsPath = HttpContext.Current.Server.MapPath("~/Reports");            return new ReportFileResolver(reportsPath)
                .AddFallbackResolver(new ReportTypeResolver());
        }        protected override ICache CreateCache()
        {
            return Telerik.Reporting.Services.Engine.CacheFactory.CreateFileCache();
        }
    }
}
   
Index.cshtml
---------------------------------
<head>
    <link href="ReportViewer/styles/ReportViewer-7.2.13.1010.css" rel="stylesheet" />
    <script src="ReportViewer/js/ReportViewer-7.2.13.1010.js"></script>    <style>
        #reportViewer1 {
            /*position: absolute;*/
            left: 5px;
            right: 5px;
            top: 5px;
            bottom: 5px;
            font-family: 'segoe ui', 'ms sans serif';
            overflow: hidden;
        }
    </style></head>
<style>
    ul {
        list-style-type: none;
        padding: 0;
        margin: 0;
    }    table td, table td * {
        vertical-align: top;
    }
</style><h3>Title</h3><table style="padding: 0; margin: 0; border: 1px">
    <tr>
        <td>
            <ul id="panelbar" style="width: 150px; list-style-position: inside; padding: 0; margin: 0">
    /* code omitted */
            </ul>
        </td>
        <td>
            <div id="reportViewer1" class="k-widget">
            </div>
         </td>
    </tr>
</table><script>
    $("#panelbar").kendoPanelBar({
        expandMode: "single"
    });
</script><script type="text/javascript">
    //async
    $(document).ready(function () {
  //code omitted for panelbar        $("#reportViewer1")
            .telerik_ReportViewer({
            serviceUrl: "/api/reports/",
            templateUrl: '/ReportViewer/templates/telerikReportViewerTemplate.htmll',
            reportSource: { report: "~Reports/Sample1.trdx" }
        });
    });
</script>

trdx file
-----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Report Width="8in" Name="Sample Report 1" xmlns="http://schemas.telerik.com/reporting/2012/3.4">
  <Items>
    <PageHeaderSection Height="0.730000019073486in" Name="pageHeaderSection1">
      <Style>
        <BorderStyle Bottom="Solid" />
        <BorderColor Bottom="0, 61, 121" />
      </Style>
      <Items>
        <PictureBox Width="1.82000005245209in" Height="0.629999995231628in" Left="6.18in" Top="0.1in" Sizing="Stretch" Anchoring="Top, Right" Name="pictureboxCompanyLogo" />
        <TextBox Width="6.07991790771484in" Left="0in" Height="0.5in" Top="0.1in" Value="" TextWrap="True" Anchoring="Top, Left, Right" Name="textBoxHeader">
          <Style Color="0, 61, 121" TextAlign="Left" VerticalAlign="Bottom">
            <Font Name="Microsoft Sans Serif" Size="12pt" Bold="True" />
          </Style>
        </TextBox>
      </Items>
    </PageHeaderSection>
    <DetailSection Height="6in" Name="detailSection1">
      <Items>
        <Table Width="1.59375in" Height="0.61666658787421in" Left="0.00in" Top="0.16in" ColumnHeadersPrintOnEveryPage="True" Name="detailTable_b066f4d8089a4c38993391bc534ce628">
          <Body>
            <Cells>
              <TableCell RowIndex="0" ColumnIndex="0" RowSpan="1" ColumnSpan="1">
                <ReportItem>
                  <TextBox Width="0.927083333333333in" Height="0.18in" CanGrow="True" WordWrap="True" Left="0in" Top="0in" Value="=Fields.[Description]" Name="textBoxDetail_Description">
                    <Style TextAlign="Left" VerticalAlign="Middle">
                      <Font Name="Microsoft Sans Serif" Size="8pt" />
                    </Style>
                    <ConditionalFormatting>
                      <FormattingRule>
                        <Style BackgroundColor="230, 231, 232" />
                        <Filters>
                          <Filter Expression="=RowNumber()%2" Operator="Equal" Value="=1" />
                        </Filters>
                      </FormattingRule>
                    </ConditionalFormatting>
                  </TextBox>
                </ReportItem>
              </TableCell>
              <TableCell RowIndex="0" ColumnIndex="1" RowSpan="1" ColumnSpan="1">
                <ReportItem>
                  <TextBox Width="0.666666666666667in" Height="0.18in" CanGrow="True" WordWrap="True" Left="0in" Top="0in" Value="=Fields.[Value]" Name="textBoxDetail_Value">
                    <Style TextAlign="Right" VerticalAlign="Middle">
                      <Font Name="Microsoft Sans Serif" Size="8pt" />
                    </Style>
                    <ConditionalFormatting>
                      <FormattingRule>
                        <Style BackgroundColor="230, 231, 232" />
                        <Filters>
                          <Filter Expression="=RowNumber()%2" Operator="Equal" Value="=1" />
                        </Filters>
                      </FormattingRule>
                    </ConditionalFormatting>
                  </TextBox>
                </ReportItem>
              </TableCell>
            </Cells>
            <Columns>
              <Column Width="0.927083333333333in" />
              <Column Width="0.666666666666667in" />
            </Columns>
            <Rows>
              <Row Height="0.18in" />
            </Rows>
          </Body>
          <Corner />
          <Style>
            <BorderStyle Default="Solid" />
          </Style>
          <RowGroups>
            <TableGroup Name="detailTableGroup">
              <Groupings>
                <Grouping />
              </Groupings>
            </TableGroup>
          </RowGroups>
          <ColumnGroups>
            <TableGroup Name="tableGroup">
              <ReportItem>
                <TextBox Width="1.59375in" Height="0.2in" Left="0in" Top="0in" Value="" Name="textBoxTableTitle_detailTable_b066f4d8089a4c38993391bc534ce628" StyleName="">
                  <Style BackgroundColor="0, 61, 121" Color="White" VerticalAlign="Middle" Bottom="Solid">
                    <BorderStyle Bottom="Solid" />
                    <BorderColor Bottom="131, 171, 85" />
                    <Font Name="Microsoft Sans Serif" Bold="False" Size="9pt" />
                  </Style>
                </TextBox>
              </ReportItem>
              <ChildGroups>
                <TableGroup Name="groupDescription">
                  <ReportItem>
                    <TextBox Width="0.927083333333333in" Height="0.23in" Left="0in" Top="0in" Value="Description" Name="textBoxRowHeader_Description">
                      <Style BackgroundColor="White" Color="13, 80, 169" VerticalAlign="Bottom" TextAlign="Left">
                        <BorderStyle Bottom="Solid" />
                        <BorderColor Bottom="13, 80, 169" />
                        <Font Name="Microsoft Sans Serif" Size="9pt" Bold="False" />
                        <Padding Bottom="1pt" />
                      </Style>
                    </TextBox>
                  </ReportItem>
                </TableGroup>
                <TableGroup Name="groupValue">
                  <ReportItem>
                    <TextBox Width="0.666666666666667in" Height="0.23in" Left="0in" Top="0in" Value="Value" Name="textBoxRowHeader_Value">
                      <Style BackgroundColor="White" Color="13, 80, 169" VerticalAlign="Bottom" TextAlign="Right">
                        <BorderStyle Bottom="Solid" />
                        <BorderColor Bottom="13, 80, 169" />
                        <Font Name="Microsoft Sans Serif" Size="9pt" Bold="False" />
                        <Padding Bottom="1pt" />
                      </Style>
                    </TextBox>
                  </ReportItem>
                </TableGroup>
              </ChildGroups>
            </TableGroup>
          </ColumnGroups>
        </Table>
        <TextBox Width="2.5in" Height="0.18in" CanGrow="False" Left="0in" Top="0.82666658787421in" Value="* Lower rate indicates better performance" Name="textBoxDetail_a4b1b395d8414d85a3e237623d8289c7">
          <Style Color="Black" TextAlign="Left" VerticleAlign="Middle">
            <Font Name="Microsoft Sans Serif" Size="8pt" Bold="False" Italic="True" />
          </Style>
        </TextBox>
      </Items>
    </DetailSection>
    <PageFooterSection Height="0.769999821980794in" Name="pageFooterSection1">
      <Style>
        <BorderStyle Top="Solid" />
        <BorderColor Top="0, 61, 121" />
      </Style>
      <Items>
        <PictureBox Width="0.8in" Height="0.5in" Left="0.1in" Top="0.15in" Sizing="Stretch" MimeType="image/png" Anchoring="Top, Left" Value="" Name="pictureBox" />
        <TextBox Width="2.69997572898865in" Height=".18in" Left="5.19791666666667in" Top="0.447916030883789in" Value="=&quot;Generated on &quot; + NOW()" Anchoring="Top, Right" Name="currentTimeTextBox" StyleName="PageInfo">
          <Style Color="DimGray" TextAlign="Right" VerticalAlign="Middle">
            <Font Name="Microsoft Sans Serif" Size="9pt" />
          </Style>
        </TextBox>
        <TextBox Width="3.0in" Height="0.2in" Left="2.5in" Top="0.15in" Value="=&quot;Page &quot; + PageNumber + &quot; of &quot; + PageCount" Anchoring="Top, Left" Name="pageInfoTextBox" StyleName="PageInfo">
          <Style Color="13, 80, 169" TextAlign="Center" VerticalAlign="Middle">
            <Font Name="Microsoft Sans Serif" Size="9pt" />
          </Style>
        </TextBox>
      </Items>
    </PageFooterSection>
  </Items>
  <StyleSheet>
    <StyleRule>
      <Style>
        <Padding Left="2pt" Right="2pt" />
      </Style>
      <Selectors>
        <TypeSelector Type="TextItemBase" />
        <TypeSelector Type="HtmlTextBox" />
      </Selectors>
    </StyleRule>
  </StyleSheet>
  <PageSettings>
    <PageSettings PaperKind="Letter" Landscape="False">
      <Margins>
        <MarginsU Left="0.25in" Right="0.25in" Top="0.25in" Bottom="0.25in" />
      </Margins>
    </PageSettings>
  </PageSettings>
</Report>

2 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 13 May 2014, 11:30 AM
Hello Glenn,

We have created a small web application that loads a .trdx report file, binds it to a DataTable and then displays the report in the HTML5 Report Viewer.
Please find the sample application in the attached file.

In order to create the demo we have used a custom report resolver. For more information, please check the How To: Implement a Custom Report Resolver help article.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Glenn
Top achievements
Rank 1
answered on 16 May 2014, 03:22 PM
Thank you.  Sorry for the delayed response.

Glenn
Tags
General Discussions
Asked by
Glenn
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Glenn
Top achievements
Rank 1
Share this question
or