We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export to PDF, XLS and Word with OData Datasource

How do you properly export your grid if you are using OData as the datasource?

I have seen plenty of examples that use the WebApi Controller with a Northwind dataContext (MS Sample db).  
For example: http://mvc.syncfusion.com/demos/web/grid/exportinggrid.

However, my data source is embedded in the grid and is a rest service (see below).  How should this be called appropriately?  

@(Html.EJ().Grid<object>("Grid")
     .AllowPaging()
        .AllowSorting()
            .Datasource(d => d.URL("https://MyOdataQuery")
                .CrossDomain(true))
            
                .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
                    {
                        items.AddTool(ToolBarItems.ExcelExport);
                        items.AddTool(ToolBarItems.WordExport);
                        items.AddTool(ToolBarItems.PdfExport);
                    }))
                        .Mappers(map => map.ExportToExcelAction("ExcelAction")
                .ExportToPdfAction("PdfAction").ExportToWordAction("WordAction")
              )
               
        .Columns(col =>
            {
                col.Field("ApplicationID").HeaderText("Application ID").TextAlign(TextAlign.Right).Add();
                col.Field("Visits").HeaderText("Visits").TextAlign(TextAlign.Right).Add();

            })
        
         .ClientSideEvents(eve => { eve.Load("load"); })
         .AllowFiltering()
)

3 Replies

BM Balaji Marimuthu Syncfusion Team August 27, 2015 01:32 PM UTC

Hi Jim,

We have analyzed your code.

Are you using the WebApiController wih Odata to the rest service or are you using OdataController only?

If you are using OdataController only, then could you please share with us the following:

1.       Share us the version of Odata details. Are you using the Odatav4.
2.       A sample if you can create and send one will be really helpful for us to locate the cause of the issue



If you are using the WebAPI controller, then please refer to the below document for helpful information:
http://helpjs.syncfusion.com/js/grid/exporting


Regards,
Balaji Marimuthu



JW Jim Woods August 28, 2015 03:37 AM UTC

I am using Odata 4.  

The call goes to a separate api.  It is not a part of the solution I am working on, rather it is a separate project/solution.  

With that in mind, are you suggesting that the export requires its own separate rest call within that api?  If so, does this mean the api requires the syncFusion dlls as well?




BM Balaji Marimuthu Syncfusion Team August 31, 2015 07:27 AM UTC

Hi Jim,


We have prepared an Exporting sample in OdataV4. Please refer to the sample and code example as below,

Sample: Exporting-Odata4

@(Html.EJ().Grid<object>("Editing")

          .Datasource(ds => { ds.URL("/odata/Orders").Adaptor(AdaptorType.ODataV4Adaptor); })

        

          .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>

                     {

                         items.AddTool(ToolBarItems.ExcelExport);

                         items.AddTool(ToolBarItems.WordExport);

                         items.AddTool(ToolBarItems.PdfExport);

                     }))

             .Mappers(map => map.ExportToExcelAction("/odata/Orders/ExcelExport")

               .ExportToPdfAction("/odata/Orders/PdfExport").ExportToWordAction("/odata/Orders/WordExport"))

        .AllowPaging()

        .Columns(col =>

        {

            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(50).Add();

            col.Field("EmployeeID").HeaderText("Employee ID").Width(50).Add();

            col.Field("ShipCity").HeaderText("ShipCity").Width(50).Add();


        })

)


Also, Map the route in WebApiConfig to perform the Export Action as follows,

config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "odata/{controller}/{action}/{id}", //if you are using odata controller

                defaults: new { id = RouteParameter.Optional }

            );


// if you are using WebApi controller


config.Routes.MapHttpRoute(

                name: "DefaultActionApi",

                routeTemplate: "api/{controller}/{action}/{id}",

                defaults: new { id = RouteParameter.Optional }

            );


Query: #1 the call goes to a separate api.  It is not a part of the solution I am working on, rather it is a separate project/solution. With that in mind, are you suggesting that the export requires its own separate rest call within that api?  


Exporting doesn’t require own separate rest call within that API. You can use it as a separate project/solution and map the Exporting Action to Grid by using Mappers.


Please refer to the following code example,


@(Html.EJ().Grid<object>("Editing")

         

             . . .


             .Mappers(map => map.ExportToExcelAction("/odata/Orders/ExcelExport")

               .ExportToPdfAction("/odata/Orders/PdfExport").ExportToWordAction("/odata/Orders/WordExport"))

        )


Query: #2 if so, does this mean the api requires the syncFusion dlls as well? 


Yes, the Syncfusion dll’s are required to perform the Exporting Operation. Please refer to the Syncfusion dll in your Exporting project/solution.


Please refer to the dependencies for Exporting from the following link:

http://helpjs.syncfusion.com/js/dependencies


Please get back to us if you need any further assistance.

Regards,

Balaji Marimuthu


Loader.
Live Chat Icon For mobile
Up arrow icon