How do I target what attachment has been updated in SaveAttachmentVersion event

Tracey Penberthy asked on October 27, 2017 17:38

Hello

I have a module in which I want to listen out for changes to the File attachment of CMS.File page types.

There are multiple attachments connected to our CMS.File page type (Secondary document and Thumbnail image)

I only want to listen out for changes to the File attachment. How do I work out if it is the File attachment that has been changed?

Here's my code

public class CMSFileExtendModule : Module
{
        public CMSFileExtendModule()
            : base("CMSFileExtenders")
        {

        }
        protected override void OnInit()
        {
            base.OnInit();

            WorkflowEvents.SaveAttachmentVersion.After += SaveAttachmentVersion_After;
        }
        private void SaveAttachmentVersion_After(object sender, WorkflowEventArgs e)
        {
            if (e.Document.ClassName == "CMS.File")
            {
                // How can I tell which attachment has been changed?
            }
        }
    }

Recent Answers


Tracey Penberthy answered on October 30, 2017 18:22

So the only way I can think to do it is to see if a CMS.File with FileAttachment that equals the e.Attachment.AttachmentGUID exists - if it does you can assume that we are updating the FileAttachment attachment (rather than the thumbnail or secondary document attachment)

var file = DocumentHelper.GetDocuments("CMS.File")
.WhereEquals("FileAttachment", e.Attachment.AttachmentGUID)
.FirstOrDefault();

if (null != file)
{
    // do your stuff
}
0 votesVote for this answer Mark as a Correct answer

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