Inserting grouped attachments

Yehuda Lando asked on February 22, 2014 12:07

Hi,

How do I insert grouped attachments in code? I'm guessing it's similar to inserting a normal attachment like:

DocumentHelper.AddAttachment(document, "Picture", Server.MapPath(path), treeProvider);
document.Update();

But in this case, I would also like to set the attachment name and description.

Is this how I do it?

                var attachmentInfo = new AttachmentInfo()
            {
                AttachmentBinary = imageBinary,
                AttachmentDescription = description,
                AttachmentName = name,
                AttachmentDocumentID = document.DocumentID,
                AttachmentExtension = extension,
                AttachmentSize = imageBinary.Length,
                AttachmentMimeType = MimeTypeHelper.GetMimetype(extension),
                AttachmentSiteID = CMSContext.CurrentSiteID,
                AttachmentGUID = Guid.NewGuid(),
            };
            AttachmentInfoProvider.SetAttachmentInfo(attachmentInfo);

Correct Answer

Pavel Janečka answered on February 23, 2014 10:30

Hi, as far as I remember (meaning this friday) I have been solving the very same issue! This is is how I done it:
var groupedAttachment = DocumentHelper.AddGroupedAttachment(node, newAttachmentGuid, attachmentGroupGuid, filePath, treeProviderObject, width, height, maxWidth); // last three args can be set to defaults

Documenthelper automatically sets your attachmentname by the filename, so now you must rename it:
groupedAttachment.AttachmentName = "your favorite attachment name";

finally, you have to update that attachment. I don't exactly remember, but this should probably be it:

AttachmentInfoProvider.SetAttachmentInfo(groupedAttachment);

I will verify this solution on Monday though.

Best regards,
Pavel Janecka

0 votesVote for this answer Unmark Correct answer

Recent Answers


Yehuda Lando answered on February 23, 2014 11:38

Thanks Pavel. I did something similar:

var tempAttachment =  DocumentHelper.AddGroupedAttachment(node, newAttachmentGuid, attachmentGroupGuid, filePath, treeProviderObject, width, height, maxWidth); // last three args can be set to defaults
node.Update();

And then:

attachmentInfo = AttachmentInfoProvider.GetAttachmentInfo(tempAttach.AttachmentGUID, CMSContext.CurrentSiteName);
attachmentInfo.AttachmentTitle = "Attachment 1";
attachmentInfo.AttachmentDescription = "Attachment 1";
AttachmentInfoProvider.SetAttachmentInfo(attachmentInfo);

I had to use the tempAttachment variable since I got the error: "File was closed" when I didn't. I think this is because my attachment is only stored on the file system.

Now I have just one problem: How to get the attachment group guid? For now I just inserted an attachment through the cms desk and then copied it's group guid, which works, but would be nice to know for the future where to get it with code.

Thanks

0 votesVote for this answer Mark as a Correct answer

Pavel Janečka answered on February 24, 2014 04:11

@YEHUDA LANDO: sorry for the delay, I didn't have my work PC at my disposal.

Now when I take a look at my source code, of course you're right with getting the attachment after inserting it, sorry for my mistake!

Anyway, as for getting the group GUID, if you use a column's GUID for that, then you can use this (actually my colleague Michal Vrana pointed me to this solution when I was facing a similar problem):

FormInfo myClass = FormHelper.GetFormInfo("your favorite classname", false);
var myColumnGuid = myClass.GetFormField("Column name you need").Guid;

And about getting the attachment, I recommend you this minor alteration: AttachmentInfoProvider.GetAttachmentInfoWithoutBinary()

Best regards,
Pavel Janecka

0 votesVote for this answer Mark as a Correct answer

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