API Questions on Kentico API.
Version 6.x > API > Can't Retrieve Custom Document Field Values View modes: 
User avatar
Member
Member
scott_hancock-urmc.rochester - 12/21/2012 2:43:14 PM
   
Can't Retrieve Custom Document Field Values
Hi,

I've created a new document type with one custom field called "Content". I'm able to access this field from my transformations just fine using "<%# Eval("Content") %>" but when I try to access this field using the API in a web part I just get null. Here is the code I'm using.
public void GetDocumentContent(Guid documentGuid)
{
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
TreeNode node = tree.SelectSingleNode(documentGuid, CMSContext.CurrentPageInfo.DocumentCulture, CMSContext.CurrentSite.SiteName);
node.GetValue("Content");
output.Text = ValidationHelper.GetString(node.GetValue("Content"), "No Data Found");
}

I've debugged the code so I know that I'm retrieving the correct document (the document is published) but the field is always null. Any idea what I'm doing wrong?

Thanks,
Scott

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 12/21/2012 9:04:34 PM
   
RE:Can't Retrieve Custom Document Field Values
Hello,

You need to specify the document type in the SelectSingleNode(...) method. otherwise only common columns for all document types are retrieved, since each document type has different custom fields. If you specify the document type you should be also able to retrieve your custom fields/columns.

For future I would recommend you to use our SQL debug to see, which SQL query is executed, so you can easier find the root cause of the issue without having to wait for support to reply.

Best regards,
Boris Pocatko

User avatar
Certified Developer 9
Certified Developer 9
pmcweb - 3/5/2014 5:13:54 PM
   
RE:Can't Retrieve Custom Document Field Values
Hi Boris,

That's true, unfortunately you're then forced to do the below, because there isn't a overload that takes both guid and class name.


public static TreeNode GetProfileNode(object nodeGuid)
{
Guid guid = ValidationHelper.GetGuid(nodeGuid, Guid.Empty);
if (guid == Guid.Empty) return null;

TreeProvider treeProvider = new TreeProvider(CMSContext.CurrentUser);
TreeNode node = treeProvider.SelectSingleNode(guid, CMSContext.CurrentDocument.DocumentCulture, CMSContext.CurrentSiteName);
if (node == null || node.ClassName != "Custom.Profile") return null;
node = treeProvider.SelectSingleNode(node.NodeID, node.DocumentCulture, "Custom.Profile");
if (node == null) return null;
return node;
}


I'm slightly worried about performance of this approach, is there a better way?

Thanks,
P.

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 3/6/2014 1:13:15 AM
   
RE:Can't Retrieve Custom Document Field Values
Hi,

Thank you for your message.

Firstly I would like to point out that the forums are obsolete and will be closed soon. (use Question and Answers instead)

To address your issue Im not quite sure why you would need the both GUID and class code name since each GUID is unique and therefore you will always get a single node. Now if you want to lets say include documents of certain class, you can use that in a simple condition like you used in your code. This should not be a performance issue as this tasks are pretty straightforward and easy.

Kind regards,
Richard Sustek

User avatar
Certified Developer 9
Certified Developer 9
pmcweb - 3/6/2014 12:14:01 PM
   
RE:Can't Retrieve Custom Document Field Values
Hi Richard,

Correct, I don't need both, but there isn't a simple override to a query by classname AND document Guid (at least not on my version of v7).

After some digging, i did find an override that will suit my needs (query a single document by guid):


TreeNode node = treeProvider.SelectSingleNode(CMSContext.CurrentSiteName, null, CMSContext.CurrentDocument.DocumentCulture, true, "Custom.Profile", string.Format("NodeGuid ='{0}'", guid), null, -1, true, null);


A bit of a 'hand-full' of unneeded arguments, any chance of a SelectSingleDocument(Guid guid) override in future releases?

Cheers,
P.

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 3/11/2014 6:26:43 AM
   
RE:Can't Retrieve Custom Document Field Values
Hi,

Thank you for your message.

Well, the first solution you offered seems better to me, even with performance view. You can use only GUID to get the document and then once you have it check the class name.

However I believe that you are doing this witl multiple document and in that case I understand the issue. Although Im not sure if there will be any overloaded method for this in the future, you can definitely create your own one or you can e.g. create your own database query.

Kind regards,
Richard Sustek