Home > Forum > Actions > Update/add attachments in Custom SDK action

Update/add attachments in Custom SDK action
0

Hello,

I'm trying to update (preferred) or add a new attachment from a custom SDK action but have been unable to do so.

The action reads the contents of a word attachment and makes changes to the content and should then update the attachment.

Reading the attachment works fine but updating or adding a new one does not seem to work.

Is there any complete and working sample code for updating/adding attachments from a custom SDK action available?

thanks!

MVP

Hi Tom

I've created some actions handling attachments. Adding an attachment should be quite easy by accessing the attachment via the context.

E.g. args.Context.CurrentDocument.Attachments.AddNew(this.Configuration.AttachmentName, filledPdf);

The variable filledPdf is of type byte[]

For updates you have to use the DocumentsAttachmentsManager.

var attachmentManager = new DocumentAttachmentsManager(args.Context);

var newAttachment = attachmentManager.GetAttachment(newAttachmentId);

Change whatever you need to change
attachmentManager.UpdateAttachment(new UpdateAttachmentParams() { Attachment = newAttachment });

In reply to: Markus Jenni

Hi Tom

I've created some actions handling attachments. Adding an attachment should be quite easy by accessing the attachment via the context.

E.g. args.Context.CurrentDocument.Attachments.AddNew(this.Configuration.AttachmentName, filledPdf);

The variable filledPdf is of type byte[]

For updates you have to use the DocumentsAttachmentsManager.

var attachmentManager = new DocumentAttachmentsManager(args.Context);

var newAttachment = attachmentManager.GetAttachment(newAttachmentId);

Change whatever you need to change
attachmentManager.UpdateAttachment(new UpdateAttachmentParams() { Attachment = newAttachment });

Thanks you. that works! Here is the whole custom action:

public override void Run(RunCustomActionParams args)
{
_args = args;
try
{
debug($"Starting RTF2Word action.");

var attachmentManager = new DocumentAttachmentsManager(args.Context);
var newAttachment = attachmentManager.GetAttachment(Configuration.RTF2WordAttachmentId);

// replace placeholder text
newAttachment.Content = ReplaceRTFContent(newAttachment.Content, @"$TEXTPLACEHOLDER$");

attachmentManager.UpdateAttachment(new UpdateAttachmentParams() { Attachment = newAttachment });

debug($" RTF2Word isPublished: " + newAttachment.IsPublished);

}
catch (Exception e)
{
info($"ERROR generating RTF2Word: {e.Message}");
args.HasErrors = true;
args.Message = e.Message;
}
finally
{
debug($"Finished RTF2Word action.");
}

}

Code seems to run fine, no errors.