Genpact Cora Knowledge Center

Support

Add an Attachment to a Form

Use the API to add an attachment to a table query of a Form Activity.

// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 
 
// References:
//
// PNMsoft.Sequence.dll
// PNMsoft.Sequence.Extensions.dll
// PNMsoft.Sequence.Forms.dll
 
 
using PNMsoft.Sequence;
using PNMsoft.Sequence.Data;
using PNMsoft.Sequence.Forms.Activities;
using PNMsoft.Sequence.Linq;
using PNMsoft.Sequence.Net;
 
namespace SequenceEx.Forms.Samples
{
    class AttachmentExample
    {
        public void AddAttachment(int activityInstanceId, string queryName, string attachmentFieldName, stringattachmentName, byte[] attachmentContent)
        {
            FormActivityExecutor executor = FormActivityExecutor.GetExecutor(activityInstanceId);
 
            using (DataContext ctx = executor.CreateDataContext())
            {
                IDataTable uact = ctx.GetDataTable(queryName);
                IDataRow row = uact.CreateRow();
 
                AttachmentEntity attachment = new AttachmentEntity();
                attachment.FileName = attachmentName;
                attachment.ContentType = MimeTypes.GetContentType(attachmentName);
                attachment.Content = attachmentContent;
                attachment.ContentLength = attachmentContent.Length;
                attachment.Id = Guid.NewGuid();
                row.SetField(attachmentFieldName, attachment);
 
 
                uact.InsertOnSubmit(row);
                ctx.SubmitChanges();
            }
        }
 
        public void AddAttachmentFromExternalConsumer(int activityInstanceId, string queryName, stringattachmentFieldName, string attachmentName, byte[] attachmentContent)
        {
            ActivityInstance activityInstance =ActivityExecutionContext.Current.WorkflowInstance.GetActivityInstance(activityInstanceId);
            FormActivityExecutor executor = new FormActivityExecutor(activityInstance);
 
            using (DataContext ctx = executor.CreateDataContext())
            {
                IDataTable uact = ctx.GetDataTable(queryName);
                IDataRow row = uact.CreateRow();
 
                AttachmentEntity attachment = new AttachmentEntity();
                attachment.FileName = attachmentName;
                attachment.ContentType = MimeTypes.GetContentType(attachmentName);
                attachment.Content = attachmentContent;
                attachment.ContentLength = attachmentContent.Length;
row.SetField(attachmentFieldName, attachment);
 
 
                uact.InsertOnSubmit(row);
                ctx.SubmitChanges();
            }
        }
    }
}