AddAttachment Method in Kentico

Andre Pfanz asked on April 14, 2016 22:45

We are using Kentico v9 and are importing blog posts from Wordpress into Kentico. We are having a problem importing the attachments into the blog post. Here's the code currently being used:

var attachmentNode = TreeNode.New(FILE_PAGE_TYPE);
attachmentNode.DocumentName = filename;
attachmentNode.DocumentCulture = postNode.DocumentCulture;
attachmentNode.Insert(postNode);
DocumentHelper.AddAttachment(attachmentNode, "ce4c5d10-c143-4ada-9d8a-7e7481b167ef", localFileLocation, postNode.TreeProvider);
attachmentNode.Update();

This does not produce any error, and there is a record in the database for the file. However, the file itself is not in Kentico. Does anyone know what I'm doing wrong here?

Correct Answer

Brenden Kehren answered on April 14, 2016 23:16

This is the code I'm using to add an attachment and it works flawlessly.

            TreeNode newNode = TreeNode.New(PageType, tree);
            newNode.DocumentName = mediaFile.FileName;
            newNode.DocumentName = fileName.Trim();
            newNode.SetValue("FileDate", fileDate);
            DocumentHelper.InsertDocument(newNode, parentNode.NodeID);

            AttachmentInfo newAttachment = null;

            // Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control).
            string filePath = MediaLibraryPath + @"\" + mediaFile.FileName + mediaFile.FileExtension;

            // Insert the attachment and update the document with its GUID
            newAttachment = DocumentHelper.AddUnsortedAttachment(newNode, Guid.NewGuid(), filePath, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE);

            // attach the new attachment to the page/document
            newNode.SetValue("FileAttachment", newAttachment.AttachmentGUID);
            DocumentHelper.UpdateDocument(newNode);
            newNode.Publish();

Let me know if this works for you or not.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Laura Frese answered on April 14, 2016 23:13 (last edited on April 14, 2016 23:21)

Are you saving the attachments on to disk or in the db? You can check by going to Settings > System > Files If you are saving them in the db, do you see the binary for the attachment? If not, Filip provides an example on this in an older version of Kentico that may work.

AttachmentInfo newAttachment = new AttachmentInfo
{
            AttachmentBinary = imageBinary,
            AttachmentDocumentID = 25,
            AttachmentExtension = "jpg",
            AttachmentSize = imageBinary.Length,
            AttachmentName = "newAttachment",
            AttachmentMimeType = MimeTypeHelper.GetMimetype("jpg"),
            AttachmentTitle = "attachmentTitle",
            AttachmentDescription = "",
            AttachmentSiteID = CMSContext.CurrentSiteID,
            AttachmentGUID = Guid.NewGuid()
};

AddAttachment(
    TreeNode node,
    string guidColumnName,
    Guid attachmentGuid,
    Guid groupGuid,
    Object file,
    TreeProvider tree,
    int width,
    int height,
    int maxSideSize
    );

If you are storing the attachments in the file system, Brenden's example should work

1 votesVote for this answer Mark as a Correct answer

Andre Pfanz answered on April 15, 2016 17:31

Thanks Brenden - that worked.

0 votesVote for this answer Mark as a Correct answer

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