Component AttachmentList

Overview

This control is meant to upload or view attachments. 


For example;
Following screenshot shows the control:


The Attachment List control uses the SysObject properties to link with the Blobs (binary files)


The Attachment List control will not update the record. It always stores the attachment in temporary blobs. System should call method commitChanges() to apply, otherwise, it never update the record.

Properties (cosmetic properties are excluded)

Following are important Properties:

Property Name

Type

Remark

AccountID, Module, ListType, ListID, OwnerID

String

These are the sysObject properties

AllowAdd

Bool

Whether to show or hide the upload button

AllowDelete

Bool

Whether to allow deletion

AllowTruncate

Bool

Whether to allow truncate

Attachments

List<AttachmentData>

The current list in the attachments

Data

List<AttachmentData>

It is the same as Attachments

MaxAttachment

Int

The number of attachment can be uploaded. Default is -1, which means unlimited


Methods 

Following are important methods:

Method Name

Type

Remark

Init(AccountID, Module, OwnerID, ListType, ListID


To initiate and also load the record if any

Init(AccountID, SysObject)


To initiate and also load the record if any

setMode_View()


To set control in view mode.

setMode_Edit()


To set control in edit mode

commitChanges()


To apply the changes. 
When user upload the attachment, it always store in Temp. By calling this method, the system will store to record (not temp anymore).

It will throw exception if something wrong

Refresh()


To reload data and refresh the control



Sample codes

Preparing Control when Record does not exists

When we want to create a new record, we usually attach the attachment list, so that we can upload the document. However, since the record has not been created, the system will upload the attachment to temporary folders. To “link” the attachment list to temp folder, do following code:


#region Attachment


AttachmentCtl.init(eCatalogContext.Current.Security.AccountID.ToString());
AttachmentCtl.MaxAttachment=2; // set max attachment to two files only

AttachmentCtl.setMode_Edit();

AttachmentCtl.Refresh();


#endregion


By assigning the AccountID only, the system knows that there’s no particular record assigned to it yet. So, any upload will be saved to temporary folder.



Preparing the Control when Record exists

Usually, we do control initialization at the Page Load event:

Assuming this.Record is the record which having SysObject, and wish to link it with the attachments.


if (!IsPostBack)

            {

                // following code will establish relationship between the Record (eCatalogObject) and the Attachment (AccountBlob)

                // do take note, it pass the CertificateAttachments property

                 AttachmentList1.init(

                             eCatalogContext.Current.Security.AccountID.ToString(), this.Record.CertificateAttachments.SysObject);


                if (this.Mode == Mode.Edit)

                {

                     AttachmentList1.loadDataFromSySobject(); // !!! Don’t forget. Load data from database based on given sysObject

                    AttachmentList1.setMode_Edit();

                }        

                else

                {

                    AttachmentList1.setMode_View();

                }


                AttachmentList1.Refresh();

               

                loadData();

            }


Once it is initialized, you can start uploading the attachment.
Even if the record is in draft (not saved to database yet), you can start uploading attachment.

The attachment will be stored in temp table.



The Record/Owner SysObject and The Attachment Relationship

Usually the Attachment list is linked to particular record. For example; the system has Doctor Record, and we want system to provide Certificate attachments for this Doctor Record. We need to establish the relationship, between Doctor Record and the Attachments (Blobs).


The Doctor Record must have property of CertificateAttachments. For example;

[JsonIgnore] // IMPORTANT TO SET

        public AccountBlobCollection CertificateAttachments

        {

            get

            {

                AccountBlobCollection oCol = new AccountBlobCollection(this.Context, this.AccountID.ToString());

                oCol.SysObject.copyDataFrom(this.SysObject); //  copy some info

                oCol.SysObject.ListID= "CertificateAttachment"; // unique list id. This is to diferentiate with other attachments

                

                // Set the default filter

                oCol.Search_AccountID = this.AccountID.ToString();

                oCol.Search_ListID = "CertificateAttachment";

                oCol.Search_ListType = this.SysObject.ListType;

                oCol.Search_Module = this.SysObject.Module;

                oCol.Search_OwnerID = this.SysObject.ObjectID;


                return oCol;

            }

        }


We can use this CertificateAttachments property, and pass it to the AttachmentList

Save the changes

System will track, which attachment is new, or deleted, or no-change.

You can call the method commitChanges(), so that system will create the record and link it to attachment.


if (RecordID == "")

            {

                oCol.Add(oRecord);  // Add the record to database

                RecordID = oRecord.ID.ToString();     // get the new ID from the just-created-record 

                oRecord = null; // reset

                AttachmentList1.SysObject = oRecord.SysObject; // Assign the attachment to the new record

 

                AttachmentList1.commitChanges();  // Apply the changes and link it to Record

            }        

            else

            {

                oCol.modify(oCall);   // save the changes record


                AttachmentList1.commitChanges();      // Apply the changes and link it to Record


                showMessage("Data has been updated");

            }

            loadData();  // reload the data