Update attachment in MVC12

Sylvain C. asked on December 14, 2020 16:31

Hello,

Using the API, I am adding new attachment to my page and it works fine:

attachment = DocumentHelper.AddAttachment(page, "DocFile", targetFilePath);
attachment.AttachmentName = file.Name;
attachment.AttachmentTitle = file.Title;
attachment.Update();

In order to keep the same GUID, I would like to now update this attachment by an other PDF ( stored in targetFilePath2) using the API:

I have tried the following but it doesn't do anything:

DocumentHelper.UpdateAttachment(page, attachment);
attachment.AttachmentUrl = targetFilePath2;
attachment.AttachmentName = file.Name;
attachment.AttachmentTitle = file.Title;
attachment.Update();

What is the correct syntax to update an attachement? How can I specify the field containing the attachment? I am able to do it in the admin UI but I can't replicate the same behavior in the API.

Thank you for your help,

Sylvain

Recent Answers


Brian McKeiver answered on December 14, 2020 17:56

Does the AttachmentBinary field contain the new binary data of the new file? You might need to call the GET first to fill the field, See https://docs.xperience.io/api12sp/content-management/attachments#Attachments-Modifyingattachmentmetadata

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on December 14, 2020 18:17

Thank you very much Brian for your help.

I have updated my code accordingly to make it work with binary:

                        byte[] fileContentBytes = file.GetContentBytes();                       
                        DocumentAttachment attachment = DocumentHelper.GetAttachment(page, file.Name);
                        //If no attachment with the same name, we add a new one
                        if (attachment == null)
                        {
                            using (var fs = new FileStream(targetFilePath, FileMode.Create, FileAccess.Write))
                            {
                                fs.Write(fileContentBytes, 0, fileContentBytes.Length);
                            }

                            attachment = DocumentHelper.AddAttachment(page, "DocFile", targetFilePath);
                        attachment.AttachmentName = file.Name;
                        attachment.AttachmentTitle = file.Title;
                        attachment.Update();                               

                        // File after being saved in the Temp folder, loaded into Kentico, is now deleted as there is a copy of it named according to its GUID in the DB
                        System.IO.File.Delete(targetFilePath);
                    }
                    //If attachment already exists, we update it so we keep the GUID which could be used by other pages
                    else
                    {
                        DocumentHelper.UpdateAttachment(page, attachment);
                        //We update the binary
                        attachment.AttachmentBinary = fileContentBytes;
                        attachment.AttachmentName = file.Name;
                        attachment.AttachmentTitle = file.Title;
                        attachment.Update();
                    }

In my case, the file comes from SharePoint, do I need to save it first as a file in order to create the attachment in the DocumentHelper.AddAttachment or can I use the binary directly from file.GetContentBytes() as I did for the update section?

Thank you

Sylvain

0 votesVote for this answer Mark as a Correct answer

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