I recently helped a client who was looking for a solution that would 
enable him to find specific attachments quickly based on their intended 
use. For example, an Account might have several attachments associated 
with it, but the user may want to know which of those various 
attachments is being used for which specific purposes. A "specific 
purpose" might be something like a contract for the account, or a 
support agreement.
In the code example below, it is assumed that a contract document 
needs to be tracked for every Account. So there is a custom field called
 ContractAttachmentId that stores the identifier of the
 Attachment containing the agreement for the Account being currently 
viewed. The code populates a drop down list with all the attachments 
associated with that Account. A user can then choose one of those 
attachments and save the record so he doesn’t have to scan through the 
entire list to find the contract.
This code should be set up on the Load event so as to populate the  dropdown as soon as the screen loads. The same concept can be extended  if other entities such as Opportunities, Contacts etc. have to be linked  to specific attachments.
string currentAttachId;
Sage.Entity.Interfaces.IAccount acct = this.BindingSource.Current as Sage.Entity.Interfaces.IAccount;
//Get a list of attachments for the current Account
Sage.Platform.Repository.IRepository repAttach  = Sage.Platform.EntityFactory.GetRepository();
Sage.Platform.Repository.IQueryable qryAttach = (Sage.Platform.Repository.IQueryable)repAttach;
Sage.Platform.Repository.IExpressionFactory efAttach = qryAttach.GetExpressionFactory();
Sage.Platform.Repository.ICriteria crAttach = qryAttach.CreateCriteria();
crAttach.Add(efAttach.Eq("AccountId", acct.Id.ToString()));
System.Collections.Generic.IList listAttach = crAttach.List();
 
if(acct.ContractAttachmentId!=null){
 currentAttachId=acct.ContractAttachmentId.ToString();
}else
{
currentAttachId="";
}
AttachmentSelection.Items.Clear();
ListItem blankItem = new ListItem("","No Selection");
AttachmentSelection.Items.Add(blankItem);
foreach(Sage.Entity.Interfaces.IAttachment attachItem in listAttach)
{
string attachDesc;
ListItem l = new ListItem();
//Truncate the attachment a little so the drop down doesnt look huge
 if(attachItem.Description.Length>30){
  attachDesc = attachItem.Description.Substring(0,30).ToString() + "...";
 }
 else{
  attachDesc = attachItem.Description;
 }
 l.Text=attachDesc.ToString();
 l.Value=attachItem.Id.ToString();
 l.Selected = l.Value.ToString().Equals(currentAttachId);
 AttachmentSelection.Items.Add(l);
}
 
      
   
            Posted in: