Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Access image attachments path in code behind View modes: 
User avatar
Member
Member
eric.savage-clyral - 10/17/2012 10:41:25 AM
   
Access image attachments path in code behind
I am trying to display an image for a document type instance. The only option seemed to be using document attachments.

Firstly, is that correct, and secondly, how can I pull out the path to that uploaded image in the code (I am using an MVC controller)?

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 1:35:08 AM
   
RE:Access image attachments path in code behind
Hi,

Which image you mean exactly by the image for document type instance? Do you mean the document type icon? If so, this is not a document attachment but a meta file. Could you please clarify your need so I can point you to the right direction?

Best regards,
Juraj Ondrus

User avatar
Member
Member
eric.savage-clyral - 10/18/2012 1:56:42 AM
   
RE:Access image attachments path in code behind
Sure. I have a document type called Paint that displays an image to the end user on the web page. So I need to let the content staff upload an image for the paint. I am pulling out this data at C# level in my MVC controller and binding it to a model, so I am trying to get the path to the image (the href) to put onto the string property on my model.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 3:21:10 AM
   
RE:Access image attachments path in code behind
Hi,

So it is a document attachment field. Thank you for clarification. In this case you need to use the API to get the attachment info. An example of the API usage is in this KB article.

Best regards,
Juraj Ondrus

User avatar
Member
Member
eric.savage-clyral - 10/18/2012 3:51:52 AM
   
RE:Access image attachments path in code behind

I am using the following code:
DataClassInfo dci = DataClassInfoProvider.GetDataClass("xxxx.Colour");
if (dci != null) {
FormInfo fi = new FormInfo(dci.ClassFormDefinition);
FormFieldInfo ffi = fi.GetFormField("Image");
Guid attGuid = ffi.Guid;


AttachmentManager am = new AttachmentManager();
AttachmentInfo ai = am.GetAttachmentInfo(attGuid, CMSContext.CurrentSite.SiteName);

But "ai" comes back as null. The example used CurrentDocument so I borrowed code from another post to get the attachment guid, which comes back with a legitimate value it seems.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 4:26:14 AM
   
RE:Access image attachments path in code behind
Hi,

I am not getting why are you using the Forms? Are we still talking about document types and not about forms? Why don't you get the node instance and then get the GUID of the document attachment field?
What is the GUID returned by your code? Does it match the GUID of the attachment?

Best regards,
Juraj Ondrus

User avatar
Member
Member
eric.savage-clyral - 10/18/2012 5:36:00 AM
   
RE:Access image attachments path in code behind
I think what's thrown me here is the fact that the Image column (the Document Attachment column) is null for all rows, even though I have attached an image via the CMS Form tab. I can confirm this if I look in the db table for the document type. There is no GUID inside there - where does the GUID come from then?

I've already tried this code, but it won't work because GetValue gives null. The node id is valid and gives the correct node back:

var node = TreeHelper.SelectSingleNode(nodeId);
Guid existingGuid = ValidationHelper.GetGuid(node.GetValue("Image"), Guid.Empty);

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 5:45:17 AM
   
RE:Access image attachments path in code behind
Hi,

What exactly you mean CMS Form tab - you mean the Form tab in CMS Desk?
What is the field type specified for the document type?
Also, I am not clear whether there is any value in the DB since these two sentences are opposite: I can confirm this if I look in the db table for the document type. There is no GUID inside there - where is no GUID?

Best regards,
Juraj Ondrus

User avatar
Member
Member
eric.savage-clyral - 10/18/2012 6:33:51 AM
   
RE:Access image attachments path in code behind
The document type is called "Colour". It has a field called "Image" with Attribute Type of "Document attachments", and the Form Control is "Document attachments control". In the db table that the CMS created for Colour, there is a column called "Image", but it only has null.

So when I add attachments, where does the attachment go? Where does the guid get stored?

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 7:02:20 AM
   
RE:Access image attachments path in code behind
Thank you for the details.

In this case you need to use DocumentHelper class and GetAttachment or GetAttachments method and one of their overloads to get the attachment.

E.g.:

DataSet attachments = DocumentHelper.GetAttachments(node, where, orderBy, false, tree);

Best regards,
Juraj Ondrus

User avatar
Member
Member
eric.savage-clyral - 10/18/2012 7:14:10 AM
   
RE:Access image attachments path in code behind
Thanks.

Where does "tree" come from? I'm using the following code which I found somewhere, but I'm missing a using reference, because I get a compilation error on "TreeProvider" not recognised.

UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);
DataSet attachments = DocumentHelper.GetAttachments(node, null, null, false, tree);

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 10/18/2012 7:32:29 AM
   
RE:Access image attachments path in code behind
Hi,

You can take a look on the API examples:

// Create a new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the example document
TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

if (node != null)
{
// Get the document's unsorted attachments with the latest on top
DataSet attachments = DocumentHelper.GetAttachments(node, where, orderBy, false, tree);


Best regards,
Juraj Ondrus