Copied to clipboard

Flag this post as spam?

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


  • Jay 409 posts 635 karma points
    Apr 03, 2016 @ 20:03
    Jay
    0

    Umbraco Forms V4.2.1 - Redirect to a media file upon form submission

    Hi,

    Had been trying out the new Umbraco V7.4.2 with the Umbraco Forms V4.2.1 and was wondering what's the best way to redirect the user upon submission of the Umbraco Forms to an uploaded media (Eg pdf file)?

    Will it be better to use a Form Workflow (if yes, where should I put the redirect code, will it be in the Execute() method?)

    Or is there any Umbraco Event that I can hook into use that detects the Form submission?

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 04, 2016 @ 06:43
    Jan Skovgaard
    1

    Hi JLon

    If you're using forms then there should be no need to make a redirect after form submission manually unless you're doing something very customized I think?

    When you click on the form in the "Forms" section you can click on the "Actions" button in the upper right corner and select the "On submit" button. Here you can create text to be displayed OR choose a page that should be shown when the form has been submitted. I guess it's that option you're looking for? :)

    Hope this helps.

    /Jan

  • Jay 409 posts 635 karma points
    Apr 04, 2016 @ 08:41
    Jay
    0

    Hi Jan,

    Yup saw that. But wanted to redirect to a media file (pdf for download). but the option only allow for content node redirection.

    Any other way?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 06, 2016 @ 19:22
    Nicholas Westby
    0

    Perhaps you could redirect to a content node that then performs another redirect to a media file?

    If you known the URL of the media file and can hardcode it, you can just do that in the CSHTML file for the form (you can create custom CSHTML files for particular forms so that not all forms share the same CSHTML file).

    If you need the URL to be dynamic, you can create a custom field type for Umbraco Forms. That field type can include a media picker. You can make it so that it doesn't render on the frontend of the website, so you'd essentially just be using this field type to attach extra data to your form. When the form is submitted, you can query this form for all of its fields to find the media picker custom field that you created to extract the URL of the media item. A simpler alternative would be to just use a hidden field that contains the URL of the media item. You can then redirect to that URL.

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 06, 2016 @ 19:36
    Nicholas Westby
    1

    Just to supplement my previous post, I thought of a slight variation that may be more optimal.

    Rather than redirecting to a content that then performs another redirect to the media file, you could actually just do it in a single redirect.

    Essentially, you'd create a custom URL provider that detects a document type of "MediaRedirect" (or whatever you want to call it). If so, you could get the URL of the media item from a media picker property on that content node then return that as the URL to use rather than the URL of the content node.

    Assuming Umbraco Forms is getting URL's in the usual way, it ought to redirect directly to the media file rather than the content node.

  • Jay 409 posts 635 karma points
    Apr 07, 2016 @ 10:49
    Jay
    0

    Cool. Let me try that out. Thanks Nicholas

  • Jay 409 posts 635 karma points
    Apr 11, 2016 @ 21:30
    Jay
    0

    Hey Nicholas works like a charm. Tested today. All sweet :)

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 11, 2016 @ 21:50
    Nicholas Westby
    0

    Glad to hear it! By the way, you might want to post some of your code here so that others can benefit from your work :-)

  • Jay 409 posts 635 karma points
    Apr 12, 2016 @ 08:34
    Jay
    1

    I've got my UrlProvider setup as below with the event's setup :) Sharing it if in case anyone is looking for the same thing like what Nicholas had recommended.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    namespace Sample.Umbraco.UrlProviders
    {
        public class FormDownloadItemUrlProvider : IUrlProvider
        {
            public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
            {
                var content = umbracoContext.ContentCache.GetById(id);
                if (content != null && content.DocumentTypeAlias == "FormDownloadItem" && content.Parent != null)
                {
                    var fileNode = content.GetPropertyValue<IPublishedContent>("filePicker");
                    if (fileNode != null)
                    {
                        // Swapping the existing URL with the File Picker's URL for form redirection
                        return fileNode.Url;
                    }
                }
                return null;
            }
    
            public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
            {
                return Enumerable.Empty<string>();
            }
        }
    }
    

    So once you've got your URL provider setup, all you need to is call that from your Umbraco Event as below

    using Sample.Umbraco.UrlProviders;
    using Umbraco.Core;
    using Umbraco.Web.Routing;
    
    namespace Sample.Umbraco.Events
    {
        public class UmbracoEvents : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {}
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //With the url providers we can change node urls.
                UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, FormDownloadItemUrlProvider>();
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {}
        }
    }
    
  • Nick Giotta 3 posts 73 karma points
    Mar 17, 2020 @ 16:16
    Nick Giotta
    0

    Does anyone have a working implementation of this for v8?

Please Sign in or register to post replies

Write your reply to:

Draft