Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Get Document by ID in macro View modes: 
User avatar
Member
Member
DanM - 10/22/2013 12:25:49 PM
   
Get Document by ID in macro
Hi,

This has been driving me crazy. I have the ID of a document in a transformation. I'm trying to create a link using the url and the name of the document.

I can use GetDocumentUrlByID() to get the url. That's the easy part. It's incredibly simple. Lovely.
The difficult part is in accessing any other of the document's fields using this ID.
I tried to use Documents[ID], but that didn't work as there doesn't seem to be a way to reference documents by their ID, only their node alias path.
If I could get the node alias path from the ID then it would be ok, but I can't find a way to do that.
Is there something that I'm missing?
I've scoured the documentation.
It would be so easy if it were possible to access a document's fields using the ID, sooooo easy.

Is they only possible way to write a custom macro function?
This does seem like it should be a basic part of the macro system.

I need to go and lay down for a while.

User avatar
Member
Member
vcarter - 10/22/2013 2:05:02 PM
   
RE:Get Document by ID in macro
Where is the data for your transformation coming from? If it is coming from the content tree, you should already have all of the documents information available to you.

If you are populating it via a custom query, then you would need to have passed along the relevant fields via your query.

Beyond that the CMSContext Class and the TreeHelper Class give you access to current page information as detailed here.

http://devnet.kentico.com/docs/devguide/index.html?cmscontext_class.htm

I believe there is a SelectSingleNode(ID) method that will also give you information.

Hope this helps.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 10/22/2013 2:27:23 PM
   
RE:Get Document by ID in macro
By default you have access to any of those fields as vcarter mentioned, if you're talking about the current document being rendered/accessed. If for instance you are talking about a child page of the current page being rendered it isn't available, you'd have to use some code for that.

If you run a query on the view that's being used to get the document data, you will see what fields and data is available to you. Try this query once in SQL Server
SELECT * FROM View_CONTENT_MenuItem_Joined
You can get this with any custom doc type as well by using
SELECT * FROM View_DocTypeNameSpace_DocTypeName_Joined

User avatar
Member
Member
DanM - 10/24/2013 7:21:33 AM
   
RE:Get Document by ID in macro
How would I go about running these queries?
Can I define a new query in the development section and then invoke it with the transformation using a macro?

I have another document (called a person document type) that is linked to a report.
This is linked by a custom field in the report.
What i wanted was to load up a link and the persons name in the transformation for the report.
So at the end of the report I would have contact : <a href="link">PersonName</a>.
The custom field gives me the ID for that person in the cms.
Using GetDocumentUrlByID() I can get the link to that report, but it seems link there is no way of retrieving the name.
I was hoping there might be a simple way of getting the name with a function, rather than going through a long winded process.
I'll try SelectSingleNode(ID) now and see if that works.

Thanks for the help.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 10/24/2013 9:01:43 AM
   
RE:Get Document by ID in macro
DanM wrote: How would I go about running these queries?
Can I define a new query in the development section and then invoke it with the transformation using a macro?
Do you have access to SQL Server Management Studio? Run it there.

User avatar
Member
Member
DanM - 10/24/2013 9:24:13 AM
   
RE:Get Document by ID in macro
I meant how do I actually go from the process of writing a query to getting that query content into my transformation?

I know how to write SQL queries, I also understand how to write c# code to create a macro (just about).

My problem is that I'm working on a live site on the azure hosted version. I've had quite a few problems with the azure hosting. Also to update the site on kentico plus I need to download a whole backup of the site, alter a few files, zip it and resend. Synching this version with the live site causes it to go offline for approx. 5 minutes.

I'm keen to have as many solutions as possible just using the CMS and portal method if possible. I was just wondering if there was a simple macro expression that I could use to get the name without going through 4 hours of work to get one on there.

For instance, finding that I could use Documents[NodeAliasPath] has been a massive help, but I couldn't really find anything about this in the documentation and stumbled upon by accident when trawling through the forums for another solution.

A seriously good guide to the Macros in Kentico, with some solid code examples would really improve my life (Are you listening, Gods of Kentico?)

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 10/25/2013 8:16:02 AM
   
RE:Get Document by ID in macro
Dan that query is already created and it is executed when your page is displayed. You will have access to all of those fields within your transformation without having to do anything.

Have you tried using
<a href="<%# GetDocumentUrl() %>"><%# EvalText("DocumentName") %></a>
This is the simplest approach I know to create a link to a document.

User avatar
Member
Member
DanM - 10/25/2013 8:48:31 AM
   
RE:Get Document by ID in macro
Hi,

I've been doing that (I use text/xml transormations, rather than ascx - as kentico support suggested I do). That is fine.

The problem is that I'm loading a list of report documents in a repeater, each report has a 'person' document ID (that links to another document on the tree) as a custom field.
So as the repeater goes through each report I can get the Document ID for this person, I would like to have a link saying -
<a href="<{% GetURLfromID(Person) %>">As written by{% GetDocNamefromID(Person) %}</a>

But at the moment I cannot get the name from just the ID using a macro in text/xml (or ascx for that matter).

So, if I could get that link AND name as a field within the transformation that would be great. At the moment there IS a function to get the link, which is brilliant. My problem is that there is no macro function to get the name and yes, I could write a custom function, etc, etc... But that's a lot of hassle for me (as explained above).

It's one of those situations where I'm so near, so so near and yet so far.
I wonder whether to think that this is a deficiency in kentico's list of macros (after all, if you are meant to write your own custom macros for this, then why is there a function that grabs the link so easily?) or if this is some kind of deficiency in me as a human being?
Yes, this CMS is beginning to engender deep existential thoughts within me. While I applaud anything that makes me scrutinise myself and my place in the world, I wish it was for reasons that were less... frustrating. Perhaps this frustration is an unavoidable precursor to questions about our own existence on this planet.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 10/25/2013 1:44:13 PM
   
RE:Get Document by ID in macro
Are you using a document selector in your Report doc type for the person? If so, you can use something like this to get a URL based on a GUID, but you'll need to create the method in code first.
/// <summary>
/// Returns a src URL from a guid
/// </summary>
/// <param name="nodeGuid"></param>
/// <returns></returns>
public static string GetUrlByGuid(string nodeGuid)
{
string ReturnValue = "";
if (!string.IsNullOrEmpty(nodeGuid))
{
string nodeGuidStr = ValidationHelper.GetString(nodeGuid, "");
Guid nodeGUID = new Guid(nodeGuidStr);
if (nodeGUID != null)
{
int nodeId = CMS.DocumentEngine.TreePathUtils.GetNodeIdByNodeGUID(nodeGUID, CMS.CMSHelper.CMSContext.CurrentSiteName);

if (nodeId > 0)
{
CMS.DocumentEngine.TreeProvider tp = new CMS.DocumentEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);
CMS.DocumentEngine.TreeNode node = tp.SelectSingleNode(nodeId);

ReturnValue = CMS.CMSHelper.CMSContext.GetUrl(node.NodeAliasPath, node.DocumentUrlPath, CMS.CMSHelper.CMSContext.CurrentSiteName);
}
}
}
return ReturnValue;
}
This way you can use what you are already using AND this method to get another URL for the person.