5

I have a requirement to change the file name of the uploaded file before saving it to the database in a predetermined naming convention "branchname_Formname". For this I need to know either the form name/form id. Sitcore.forms.MVC.ViewModels.Fields.FileUploadField is the class which is getting hit when you click submit. Here there is no property related to Forms. And also unable to fetch it using Sitecore.Context as well. Can someone provide me suggestions on how to retrieve the formname and sitename after you submit the form.

3

I had to do something similar to do with this a while ago and had some problems due to the value of "WFM.IsRemoteActions" so make sure you have that set correctly:

Reading a value from an WFFM Upload file field

Anyway once you have that set correctly your form Id and file upload values should come through correctly so you can create a save action similar to the one I've written for you below to read the values and then hopefully rename them accordingly:

public class RenameAttachment : SendMessage
{
    public RenameAttachment(ISettings settings, IMailSender mailSender) : base(settings, mailSender)
    {
        Assert.IsNotNull((object)settings, "settings");
        Assert.IsNotNull((object)mailSender, "mailSender");
        this.mailSender = mailSender;
        this.emailAttributes = (IEmailAttributes)new EmailAttributes(settings.EmailFromAddress, settings.MailServer, settings.MailServerUserName, settings.MailServerPassword, settings.MailServerPort);
    }

    public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
    {
        try
        {
            ProcessMessageArgs processMessageArgs = new ProcessMessageArgs(formId, adaptedFields, this.MessageType, this);

            processMessageArgs.Recipient = this.Recipient;
            processMessageArgs.RecipientGateway = this.RecipientGateway;
            processMessageArgs.IsBodyHtml = this.IsBodyHtml;

            List<Attachment> attachments = RetrieveAttachments(formId, adaptedFields);

            //loop attachments here and rename file using with formId above

            processMessageArgs.Attachments.AddRange(attachments);

            CorePipeline.Run("processMessage", (PipelineArgs) processMessageArgs);
        }
        catch (Exception ex)
        {
            Log.Error("Error with RenameAttachment WFFM save action", ex, this);
            throw ex;
        }
    }

    private List<Attachment> RetrieveAttachments(ID formId, IEnumerable<AdaptedControlResult> fields)
    {
        var attachments = new List<Attachment>();
        Item item = Sc.Context.Database.Items[formId];
        if (item != null)
        {
            var mediaItemIds = new List<string>();
            var formItem = new FormItem(item);

            foreach (AdaptedControlResult field in fields)
            {
                GetMediaIds(formItem, field, mediaItemIds);
            }

            GetAttachmentsFromMediaItems(mediaItemIds, attachments);
        }

        return attachments;
    }
 }

I'm assuming Sitecore 8.x here by the way as a number of things changed in WFFM since 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.