Extend FormResponseFilePathStrategy for MOVEit

Overview

This article describes how to change the way file paths are generated for files that you transfer and store in MOVEit. The folder structure in MOVEit depends on the file path. By default, all files uploaded through a specific form are stored in the library for that form. This means that in MOVEit, they are located in a single folder. This makes it difficult to associate already uploaded file with a specific form response. If you want to have control on the folder structure of the uploaded files in MOVEit, you can implement custom FormResponseFilePathStrategy as described below.

Implement custom FormResponseFilePathStrategy

To extend FormResponseFilePathStrategy, you must register a class in the ObjectFactory that inherits from FormResponseFilePathStrategy.
You can override the following methods:

  • string GetRelativeFilePath(FormDescription form, FormEntry response, string originalFileName, string fileUploadFieldName, out string description)
    This method allows you to control the folder structure of the uploaded file. The returned path is a string of segments, separated by backslashes (/). The last segment in the path is the file name. To build the custom path, you can use data from the current context, such as current site or current language.
    The following parameters provide more specific context about the form submission:
    • form - (FormDescription) the metadata of the form
    • response - (FormEntry) the data from the other fields in the form response
    • originalFileName - (string) the original name of the uploaded file
      By default, it is used as the last segment in the path.
    • fileUploadFieldName - the name of the field through which the file is uploaded
      If you have more than one FileUpload fields in the form, the method will be executed for each of them with a different value of this parameter.
    • description - an output parameter that returns additional information about the uploaded file, besides the returned path
  • string GetLibraryName(FormDescription form)
    This method allows you to control the name of the library that is created for the specified form. It is called when a form with a FileUpload field is published for the first time. Unlike the GetRelativeFilePath method described above, this method does not use current context data, because it would not be relevant to the uploaded file.

Code sample

To extend FormResponseFilePathStrategy for your MOVEit integration, implement the following code in the Global.asax file of your project:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Forms.Model;
using Telerik.Sitefinity.Modules.Forms;
using Telerik.Sitefinity.Security.Claims;
using Telerik.Sitefinity.Services;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {

       protected void Application_Start(object sender, EventArgs e)
       {
           ObjectFactory.Initialized += ObjectFactory_Initialized;
       }

       private void ObjectFactory_Initialized(object sender, ExecutedEventArgs e)
       {
           ObjectFactory.Container.RegisterType<FormResponseFilePathStrategy, CustomFormResponseFilePathStrategy>();
       }

       class CustomFormResponseFilePathStrategy : FormResponseFilePathStrategy
       {
           public override string GetLibraryName(FormDescription form)
           {
               return base.GetLibraryName(form);
           }

           public override string GetRelativeFilePath(FormDescription form, FormEntry response, string originalFileName, string fileUploadFieldName, out string description)
           {
               // Build a list of folders and return them as a path
               var folders = new List<string>();

               // Add a folder from the current context, for example, the name of the current site
               folders.Add(SystemManager.CurrentContext.CurrentSite.Name);

               // Add a folder from a value of a specific field in the form response, for example, a value from a field with 'application' field name.
               var fields = TypeDescriptor.GetProperties(response);
               var applicationField = fields["application"];
               if (applicationField != null)
                   folders.Add(applicationField.GetValue(response).ToString());

               // Add the currently logged username (the user submitting the response)
               var identity = ClaimsManager.GetCurrentIdentity();
               if (identity != null)
                   folders.Add(identity.Name);

               // Add the name of the file, for example, the name of the fileUpload field
               folders.Add(fileUploadFieldName);

               // As the original file name was replaced, it can be tracked as description of the file
               description = "The original name of the file was '{0}'".Arrange(originalFileName);

               // Return as a path
               return string.Join("/", folders.ToArray());
           }
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.