Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How to use existing attachment on another document? View modes: 
User avatar
Member
Member
Blasty - 5/4/2010 2:31:47 PM
   
How to use existing attachment on another document?
Ok, so I'm trying to write some functionality into a webpart using C#.

Basically I have a document that may or may not have an attachment.

I also have another document that definately has an attachment.

I want to take the attachment associated with the 2nd document, and assign it to the first document instead.

So if my first document has an attachment, I use this to get rid of said attachment.
DocumentHelper.DeleteAttachment(node, oldGuid, tree);

Now, How do I attach the other Documents attachment to it.
All the references for AddAttatchment want an httpPostedfile, which I don't have, it's already in the database. I got the GUID for the attachment I want to attach...how do I use it.?

I thought to try
DocumentHelper.UpdateAttachment(node, oldGuid, newGuid);

But this doesn't work if there wasn't an old Guid that's being replaced.

User avatar
Kentico Developer
Kentico Developer
kentico_martind - 5/5/2010 6:22:40 AM
   
RE:How to use existing attachment on another document?
Hello,

Basically you need to set DocumentID of new document into 'AttachmentDocumentID' field of attachment and also set GUID of attachment into field where the attachment is stored in document. Please see sample code bellow:
CMS.SiteProvider.UserInfo ui = CMS.SiteProvider.UserInfoProvider.GetUserInfo("administrator");
CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);

CMS.TreeEngine.TreeNode newNode = tree.SelectSingleNode(CMSContext.CurrentSite.SiteName, "/newNode", "en-us");

// Create the attachment manager
CMS.FileManager.AttachmentManager am = new CMS.FileManager.AttachmentManager(tree.Connection);
CMS.FileManager.AttachmentInfo ai = am.GetAttachmentInfo(guidOfAttachment, "siteName");

ai.AttachmentDocumentID = newNode.DocumentID;
newNode.SetValue("NameOfColumnWithAttachment", ai.AttachmentGUID);

Best Regards,
Martin Dobsicek

User avatar
Member
Member
Blasty - 5/5/2010 9:00:00 AM
   
RE:How to use existing attachment on another document?
that makes sense.