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 32 posts 135 karma points
    Dec 16, 2020 @ 09:51
    Duncan Turner
    0

    WorkflowType: Output Word Document to client

    I have my class file that deals with building my word document from umbraco form values, it uses DocX by Xceed.

    Calling it from a WorkflowType class, I cannot get my head around how I can supply this newly created word document to the client.

    Hoping someone can point me in the right direction?!

    Code in WorkflowType below;

    public class UmbracoFormSaveAsWord : WorkflowType {

        public UmbracoFormSaveAsWord()
        {
            Id = new Guid("a420d69f-db9e-41b5-8533-a03be70c3b3c");
            Name = "HWL: Export As Word Document Workflow";
            Description = "HWL: Export Form as a word document.";
            Icon = "icon-document";
            Group = "Services";
        }
        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            LogHelper.Debug<UmbracoFormSaveAsWord>("the IP " + record.IP + " has submitted a record");
    
            var formResults = FormResults(e);
    
            foreach (var item in e.Form.Pages)
            {
                GetFormData(item, formResults);
            }
    
    
            var wordDoc = new BuildForm();
            wordDoc.Build(formResults);
    
            LoggerMethod(record, e);
    
            return WorkflowExecutionStatus.Completed;
        }
    
    
        private void GetFormData(Page item, Results formResults)
        {
            if (item.FieldSets == null) return;
    
            var responses = new List<QuestionAnswersResponse>();
            var sections = new List<Sections>();
    
            foreach (var fieldSet in item.FieldSets)
            {
                AddUserResponsesToListOfResponses(fieldSet, responses);
    
                AddNewSection(fieldSet, responses, sections);
    
                responses.Clear();
            }
            formResults.SectionResults = sections;
        }
        private Results FormResults(RecordEventArgs e) => new Results
        {
            FormName = e.Form.Name,
            IntroductionText = "",
        };
        private void AddUserResponsesToListOfResponses(FieldSet fieldSet, List<QuestionAnswersResponse> responses)
        {
            foreach (var qas in fieldSet.Containers[0].Fields)
                responses.Add(UserResponses(fieldSet, qas));
        }
        private void AddNewSection(FieldSet fieldSet, List<QuestionAnswersResponse> responses, List<Sections> sections)
        {
            var section = new Sections
            {
                Heading = fieldSet.Caption,
                Responses = responses.Clone(),
            };
            sections.Add(section);
        }
        private QuestionAnswersResponse UserResponses(FieldSet fieldSet, Field field) => new QuestionAnswersResponse
        {
            HeadingCategory = fieldSet.Caption,
            Question = field.Caption,
            FieldType = FieldTypeFactory.GetType((Types.StripFieldTypeName(field.FieldType.FieldTypeViewName)).ToLower()),
            DataType = DataTypeFactory.GetType((field.FieldType.DataType.ToString()).ToLower()),
            Answer = field.Values.Count > 0 ? field.Values[0].ToString() : "",
            Answers = field.Values ?? new List<object>(),
        };
        private void LoggerMethod(Record record, RecordEventArgs e)
        {
            record.State = FormState.Approved;
    
            LogHelper.Debug<UmbracoFormSaveAsWord>("The record with unique id " + record.UniqueId +
                " that was submitted via the Form " + e.Form.Name +
                " with id {FormId} has been changed to " + e.Form.Id + " state approved");
        }
        public override List<Exception> ValidateSettings()
        {
            return new List<Exception>();
        }
    }
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Dec 16, 2020 @ 11:29
    Steve Morgan
    1

    Hi,

    I think I would create a folder under /app_data/ called documents then for each file create a folder with a unique guid then build a custom controller that serves up word docs via some route - e.g. www.mydomain.com/document/docPath=[[guid/filename]]

    Or attach it to an email?

    Depends on how secure you need these files to be but the guid would ensure that paths aren't guessable.

    Steve

  • Duncan Turner 32 posts 135 karma points
    Dec 16, 2020 @ 15:39
    Duncan Turner
    0

    Hi Steve,

    Thanks for getting back to me so quick!

    Would I call this custom controller to serve up the new file from WorkflowType?

    Sorry if it sounds like a dumb question.

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Dec 17, 2020 @ 15:14
    Steve Morgan
    0

    Hi,

    Just so I understand the process... you have a webpage with an Umbraco form on it. You're creating a workflow that will take the form submission and then generates this word doc - is that right?

    I guess you could redirect to the custom controller that will then serve up that form as a download???

  • Duncan Turner 32 posts 135 karma points
    Dec 17, 2020 @ 16:20
    Duncan Turner
    0

    Hi,

    Yes that is indeed a correct assumption. I shall try that.

    Thanks

  • Duncan Turner 32 posts 135 karma points
    Jan 07, 2021 @ 11:15
    Duncan Turner
    0

    Okay I am still having issues with this... As an example I have a word docx stored in a temp folder to see if it serves, but unfortunately doesn't.

    My Workflow calls an ActionResult and it and it does step through find but never serves up the document to the client.

    Have to admit that I am now stuck.

    public ActionResult Get() {           
            var file_name = "return_to_work_record.docx";
            var file = HostingEnvironment.MapPath("~/temp/" + file_name);
    
            byte[] fileBytes = System.IO.File.ReadAllBytes(file);
            return File(fileBytes, MediaTypeNames.Application.Octet, file_name);}
    

    Hopefully someone can help...

  • Duncan Turner 32 posts 135 karma points
    Jan 14, 2021 @ 14:03
    Duncan Turner
    0

    Even something as simple as this doesn't work;

    public ActionResult Get()
            {
                MemoryStream ms;
    
                using (ms = new MemoryStream())
                {
                    using (var document = DocX.Create(ms, DocumentTypes.Document))
                    {
                        // 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");
            }
    
  • Duncan Turner 32 posts 135 karma points
    Jan 15, 2021 @ 09:12
    Duncan Turner
    0

    Building a simple MVC project and running this works, so why does an umbraco project differ?

    I have tried both SurfaceController and Controller inheritance..

Please Sign in or register to post replies

Write your reply to:

Draft