Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Duncan Turner 20 posts 120 karma points
    Jan 25, 2021 @ 15:09
    Duncan Turner
    0

    Export to Word, CSV file

    I have posted elsewhere but no real answer; https://our.umbraco.com/forum/using-umbraco-and-getting-started/104532-workflowtype-output-word-document-to-client

    Is it possible to serve up a word/csv file from the Umbraco Forms submission using a workflow?

    At the moment I have the workflow call a controller and it steps through okay but never serves this to the client.

    I have tested the functionality in a mock MVC project and are successful.

    Can we do this in Umbraco?

  • Markus Johansson 1696 posts 4865 karma points c-trib
    Jan 25, 2021 @ 21:22
    Markus Johansson
    0

    Good and interesting question! :)

    From my understanding there is some idea that the workflows should be able to be fired async (like in a new thread) there’s some setting for this in the co figs. Given this there workflows would not have any access to the current HttpRequest. While this is a concept it does not really work so I’ve always been running in “synchronous” mode.

    In this mode you should have access to the HttpContext via HttpContext.Current, which maybe means that you could populate the request body or maybe set a redirect to your controller to serve the file.

    This would depend on when in the pipeline the redirect from Forms is set.

    Are you following what I’m thinking?

  • Duncan Turner 20 posts 120 karma points
    Jan 26, 2021 @ 09:28
    Duncan Turner
    0

    Hi Markus,

    Thank you for getting back to me on this.

    "HttpContext.Current, which maybe means that you could populate the request body or maybe set a redirect to your controller to serve the file."

    So at this point in the workflow it is calling/redirecting to the controller to serve the file, but doesn't.

    I tried introducing this to the controller:

     MemoryStream ms;
    
    
            //using (streamReader = new StreamReader(System.Web.HttpContext.Current.Request.InputStream))
            using (ms = new MemoryStream())
            {
                using (var document = DocX.Create(ms, DocumentTypes.Document))
                {
    
                    System.Web.HttpContext.Current.Request.InputStream.CopyTo(ms);
                    ms.Position = 0;
                    using (StreamReader stream = new StreamReader(ms))
                    {
                        // Add a title
                        document.InsertParagraph("Adding Custom Properties to a document").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center;
    
                        //Add custom properties to document.
                        document.AddCustomProperty(new CustomProperty("CompanyName", "Xceed Software inc."));
                        document.AddCustomProperty(new CustomProperty("Product", "Xceed Words for .NET"));
                        document.AddCustomProperty(new CustomProperty("Address", "3141 Taschereau, Greenfield Park"));
                        document.AddCustomProperty(new CustomProperty("Date", DateTime.Now));
    
                        // Add a paragraph displaying the number of custom properties.
                        var p = document.InsertParagraph("This document contains ").Append(document.CustomProperties.Count.ToString()).Append(" Custom Properties :");
                        p.SpacingAfter(30);
                        document.Save();
    
                    }
    
                }
            }
    
            return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Test.docx");
    

    I don't think I get it tbh...

  • Markus Johansson 1696 posts 4865 karma points c-trib
    Jan 26, 2021 @ 09:35
    Markus Johansson
    0

    Hi!

    Are you saying that you've managed to perform a redirect to the controller from the workflow but that the file is not served?

    Does the controller work when you hit it directly?

  • Duncan Turner 20 posts 120 karma points
    Jan 26, 2021 @ 09:59
    Duncan Turner
    0

    Hi,

    No sorry, I can simply call that controller from the workflow, but stepping(debugging) through the controller code the file is never served, unlike the mock MVC project I build which indeed served the file.

    Workflow -> Controller(simple standard MVC Controller)

    Are you referring to doing this when calling controller; https://our.umbraco.com/Documentation/Reference/Routing/custom-controllers-v7

  • Markus Johansson 1696 posts 4865 karma points c-trib
    Jan 26, 2021 @ 10:10
    Markus Johansson
    0

    Hi!

    Hmm, I'm not a 100% sure that I understand you, but let me try. It feels like we're talking about two things, both how to get the workflow to return a redirect to a controller and how to get the controller to serve a file.

    As I understand:

    You have another (non Umbraco project) where you have this controller and serving the files works, but you haven't manage to get this working with Umbraco?

    What kind of controller are you trying to use? A "RenderMvcController"?

    You could use a "regular" controller (inherit the standard mvc Controller-class) and route this.

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/101128-need-to-use-standard-mvc-controller

    Then, like I wrote, I'm not 100% sure that you can redirect from the workflow to this controller but it might be possible by setting up the HttpContext from your workflow.

  • Duncan Turner 20 posts 120 karma points
    Jan 26, 2021 @ 10:15
    Duncan Turner
    0

    Hi,

    Thank you for not giving up on me :-)

    "You have another (non Umbraco project) where you have this controller and serving the files works, but you haven't manage to get this working with Umbraco?" Yes

    "What kind of controller are you trying to use? A "RenderMvcController"?" Yes

    "You could use a "regular" controller (inherit the standard mvc Controller-class) and route this. https://our.umbraco.com/forum/using-umbraco-and-getting-started/101128-need-to-use-standard-mvc-controller Then, like I wrote, I'm not 100% sure that you can redirect from the workflow to this controller but it might be possible by setting up the HttpContext from your workflow."

    Thank you very much will give that a try..

  • Duncan Turner 20 posts 120 karma points
    24 days ago
    Duncan Turner
    100

    I have now got this to work, concept for anyone else that is doing something similar.

    Workflow containing a 'RedirectToAction' (my method name) method that uses the System.Web.HttpContext,Current command, then serializes the object data and sends to ActionResult controller.

    In the ActionResult controller which inherits a surface controller deserializes the object and uses your method to build your word document and return a File type.

    I have a js code in the Form.cshtml file that presents a modal popup from bootstrap that allows the user to restart the umbraco form again with a simple redirect.

    Hope it helps someone out there.

Please Sign in or register to post replies

Write your reply to:

Draft