Upload PDF via code

Regis Wengel asked on July 16, 2019 18:20

We have a client that wants us to build a form for them. Due to several requirements we are unable to use Kentico's form builder and thus are building the form via code. We have built a Kentico form in order to hold the form data and are using BizFormItem in order to create a new record for this form.

One of the fields on this form needs to be an upload file. I have been looking through the documentation, but I'm not finding anything in regards to uploading a file that is linked (attached?) to a form record. Does Kentico have any API methods that achieve this?

Correct Answer

Brenden Kehren answered on July 16, 2019 18:29

This article is quite old but the concept is very similar in newer versions. You will have to check namespaces and probably a method or two have changed but should get you in the right direction.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Regis Wengel answered on July 17, 2019 18:49

Hey Brenden,

That was exactly what I was needing, thank you. A follow up question. I also need to attach these uploaded document to the notification email. My code looks as follows

private void SendEmail(BizFormInfo bfi, DataClassInfo dci, int id)
{
    if (!string.IsNullOrEmpty(bfi.FormSendFromEmail) && !string.IsNullOrEmpty(bfi.FormSendToEmail))
    {
        bfi.FormEmailAttachUploadedDocs = true;

        BizForm f = new BizForm();

        IDataClass content = DataClassFactory.NewDataClass(dci.ClassName, id);

        f.SendNotificationEmail(
            bfi.FormSendFromEmail,
            bfi.FormSendToEmail,
            content, bfi);
    }
}

The email sends fine, however the documents don't get attached. Taking a look at CMS.OnlineForms.Web.UI.dll I found the SendNotificationEmail method.

public virtual void SendNotificationEmail(string fromEmail, string recipients, IDataContainer data, BizFormInfo bfi) {
...
if (bfi.FormEmailAttachUploadedDocs)
            {
                string bizFormFilesFolderPath = FormHelper.GetBizFormFilesFolderPath(base.SiteName, null);
                foreach (string uploadFile in this.uploadFiles)
                {
                    string str2 = Convert.ToString(data.GetValue(uploadFile));
                    if (string.IsNullOrEmpty(str2))
                    {
                        continue;
                    }
                    string str3 = string.Concat(bizFormFilesFolderPath, FormHelper.GetGuidFileName(str2));
                    if (!File.Exists(str3))
                    {
                        continue;
                    }
                    Attachment attachment = new Attachment(FileStream.New(str3, FileMode.Open, FileAccess.Read), this.GetFileNameForUploader(str2));
                    emailMessage.Attachments.Add(attachment);
                }
            }

...
}

I can't debug the dll, but my best guess is that this.uploadFiles isn't returning anything. Currently I'm saving the file to FormHelper.GetBizFormFilesFolderPath(SiteContext.CurrentSiteName); and then saving the string reference (<GUID>.<extension>/<orig_name>.<extension>) to the form. I'm guessing though that Kentico doesn't know I want to link a file to a form. Is there a method to achieve this?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.