Managing attachments

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The documents may contain a custom field of type "file". The field itself does not contain attachment data. Only AttachmentGUID (unique identifier) value is stored in the document field referencing the record within separate table.

 

The attachments are stored in the table CMS_Attachment which contains the attachment metadata (file name, size, type, etc.) and optionally the attachment binary data (depending on where the files are stored). Attachment file may also be stored in the filesystem if the system is configured for storing files in the database. You can work with the attachments using the AttachmentManager class methods.

 

Here are some examples how to work with attachments:

 

Adding a new attachment to a document

 

To add a new attachment to a document, you need to save the attachment and store its GUID (unique identifier) to the document field.

 

[C#]

 

using CMS.SiteProvider;

using CMS.TreeEngine;

using CMS.CMSHelper;

using CMS.FileManager;

 

// Always work with some user when editing documents

UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

TreeProvider tree = new TreeProvider(ui);

 

// First, get the document

CMS.TreeEngine.TreeNode node =

tree.SelectSingleNode(CMSContext.CurrentSite.SiteName, "/Files/home", "en-us");

if ( node != null )

{

// Create the attachment manager

 AttachmentManager am = new AttachmentManager(tree.Connection);

 

// Create the attachment

Guid attachmentGuid = Guid.NewGuid();

 AttachmentInfo ai =

  new AttachmentInfo("c:\\home.gif", node.DocumentID, attachmentGuid, tree.Connection);

 am.SetAttachmentInfo(ai);

 

// Set the attachment reference

 node.SetValue("FileAttachment", attachmentGuid);

 node.Update();

}

 

Removing an attachment from a document

 

To remove an existing attachment from a document, you need to delete the attachment and remove the document field reference to it. You should always remove the old attachment if you insert a new one.

 

[C#]

 

using CMS.GlobalHelper;

using CMS.FileManager;

using CMS.CMSHelper;

 

// ... this code continues from the previous example

 

// Get current attachment GUID

Guid existingGuid = ValidationHelper.GetGuid(node.GetValue("FileAttachment"), Guid.Empty);

if (existingGuid != Guid.Empty)

{

// Get the attachment

 AttachmentInfo existingAttachment =

   am.GetAttachmentInfo(existingGuid, CMSContext.CurrentSite.SiteName);

if ( existingAttachment != null )

 {

  // Delete the attachment

   am.DeleteAttachmentInfo(existingAttachment.AttachmentID);

 }

 

// Clear the attachment reference from the document field

 node.SetValue("FileAttachment", null); ;

 node.Update();

}

 

Please note: if you use workflow and versioning, see Versioning internals and the related chapters for more details how to work with attachments in the versioned documents.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?managing_attachments.htm