locked
Report invoice Help - call web-api controller from button in HtmlClient RRS feed

  • Question

  • Hello,

    I need to create a simple csv output of a table from my html client. I found this article for the web desktop client. 

    http://blog.pragmaswitch.com/?p=773

    I need to convert the following code into what I assume would be a function call using a promise object and I can use the ReportEngine and ReportController as is?

    string lastNameSearchParam = this.ShowInputBox("Give Last name", "Create Invoice");
    
        Dispatchers.Main.Invoke(() =>
        {
            Uri baseAddress = LightSwitchCommandProxy.GetBaseAddress();
    
        string url = baseAddress.AbsoluteUri +  @"api/Report/CustomersByLastName/?lastName=" + lastNameSearchParam;
        HtmlPage.Window.Navigate(new Uri(url), "_blank");
    });
    Thanks

    

    Friday, March 6, 2015 5:29 PM

Answers

  • Hi,

    when you want to create a simple CSV File from your HTML Client I would prefer using a ashx (generic handler) for this kind of things.

    You can create a button on your Screen and in the execute Tap subroutine insert something like this:

    myapp.yourscreen.ShowDetails_execute = function (screen) {
        var paramValue = screen.anyvalue; //a possible parameter
        $.ajax({
            type: 'post',
            data: {
                oparaid:  paramValue,
            },
            url: '../WebRequests/yourhandlername.ashx',
            success: function success(result) {
                // Show result
                if (result != "") {
                    element.innerText = result;
                }
                else {
                    element.innerText = result;
                }
            }
        });
    };

    In the ashx you have to make your work in the ProcessRequest Routine

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    	Dim erg As String = ""
        Dim myParameter As String = ""
    
        Try
    		myParameter = Convert.ToString(context.Request.Params("oparaid"))
            if myParameter = "" Then
                Throw New Exception("There was no parameter")
            End If
            Using Ctx As ServerApplicationContext = ServerApplicationContext.CreateContext()
                Dim ocsvdata = Aggregate b1 As anytable In Ctx.DataWorkspace.data.table Where b1.table.Id = CInt(myParameter)
                'Write CSV File
    
    		erg = "Csv File is written"
             End Using
        Catch ex As Exception
            erg = ex.Message
        End Try
        context.Response.ContentType = "text/plain"
        context.Response.Write(erg)
    End Sub

    This should be all. Translating this into C# should not be an issue.
    Hope this helps

    Kind Regards
    Thomas

    • Marked as answer by Angie Xu Monday, March 16, 2015 5:46 AM
    Saturday, March 7, 2015 4:35 PM