Get version content of a document on selection of a date

Binod Paikaray asked on April 26, 2017 16:17


I need your help! In my project, we are showing a preview of hierarchical documents in a single page which can contain many documents. But now we want to pass a date from calendar and get the latest version of the documents' by that date. The "GetDocuments()" method gives only the latest version and "VersionManager.GetVersion()" method requires VersionHistoryId as a paremeter which I don't have available as per current scenario.

So, need your help that how can I get the versioned document along with the content with passing the selected date?

Thanks in anticipation!

Binod Paikaray

Recent Answers

Binod Paikaray answered on April 26, 2017 20:15 (last edited on April 26, 2017 20:15)

Can I please get some help on this?

0 votesVote for this answer Mark as a Correct answer

Binod Paikaray answered on April 27, 2017 07:44

May I please know that whether it is doable or not?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 27, 2017 14:56

So without going through and figuring it out and writing your code myself, here are the high level steps as I'm not aware of being able to get version history for a page simply by a date AND have it be readable or ready for your public site:

  • Get the class info based off of the class name, you'll need the ClassID to get the version history
  • Get the DocumentID and date from the current rendered record
  • Query the VersionHistoryInfoProvider.GetVersionHistories() and add a where statement to narrow down by VersionClassID, DocumentID and then one or many of the date fields PublishFrom, PublishTo, WasPubishedTo, WasPublishedFrom, ModifiedWhen
  • Lastly when you get a record or records back you'll have to narrow it down to 1 AND then parse the NodeXML field to get your data.

In addition to that, you will have to determine if the document is in workflow as well, if it is, you'll have to look at the Workflow API to make sure you get the proper workflow step and version.

So the simple answer to your question, is no I'm not aware of a simple method which gets the version history based off of a date for a document.

0 votesVote for this answer Mark as a Correct answer

Binod Paikaray answered on May 8, 2017 16:19

Thanks very much Brenden for your help! I was also trying something similar to this but now stuck at some point and seeking for help.

After this code,

where = "DocumentID = " + dr["DocumentID"] + " AND ModifiedWhen <= '" + selectedDateTime + "'";
versionHistory = VersionHistoryInfoProvider.GetVersionHistories(where, orderBy, topN);

I get only the Document fields but not TreeNode fields and so I again had to use the below code after the above one.

int versionHistoryId = (int)versionHistory.Tables[0].Rows[0]["VersionHistoryID"];
TreeNode nodeVersion = VersionManager.GetVersion(versionHistoryId, null);

And then I'm converting the above result to a DataSet using the following code,

TreeNodeDataSet trnd = new TreeNodeDataSet(nodeVersion.GetValue("ClassName").ToString(), nodeVersion.GetDataSet());

Then I assign this dataset to a repeater control and inside the repeater_ItemDataBound event, I assign the current DataRow to the CMSRepeater as DataSource along with the transformation name.

Now the problem is that there is neither any AttachmentBinary column nor any such binary data in the above dataset but still the repeater is previewing the attached Figure on the page which is again required for me but it's always showing the latest version figure and not the selected version figure.

After checking few things in database, I got to know that the AttachmentGUID is always same be it for the latest version or the selected version and somehow it's always picking the binary from the table CMS_Attachment instead of CMS_AttachmentHistory and I'm not able to find that where exactly it's picking the binary even when there is no such column in the assigned DataSet and no such code written as well. Is this something done by CMSRepeater itself or by Transformation? And if it's done by Transformation then I would request you to please guide to customise same.

Thanks very much in anticipation!

Binod Paikaray

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 8, 2017 16:23

Before I get too far into this (which it seems you may have already), I'd look at some of the out of the box code that Kentico has already written to get this information. For instance if you can find the code in the file system that delivers the preview of a page you should be able to find a lot of the code you're looking for.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on May 8, 2017 16:54

So looking over the requirements, Brenden has the right idea, but this may help you out further. In my opinion, You have then two options:

1: Use the Kentico API to get each individual history piece (the document history + Tree Node Data + Attachment History Binaries), then create your own DataTable that combines the columns and rows so you have a single DataTable with all the information you want, and pass that to the cms:QueryUniView and transform

2: (Easier) if you can rig up an entire SQL Query that can gather and join all the information together (history, attachment history, tree node, etc), you can use the Universal Viewer with Custom Query to display that (with hierarchy support). ​ You see, all a Repeater is (any type of them) is a processor of a DataTable. "Page Types" just look for a ClassName column in the datatable. Hierarchy data just looks for a Parent ID column (NodeParentID), a level column (NodeLevel), and an order column (NodeOrder) in order to display in a hierarchy fashion.

You can actually trick the entire system out by providing this information. I have a custom web part called the "Universal API Viewer" in the marketplace, and that's exactly what i did with it. I took an XML / JSON feed, made some classes that set the "Class Name" "NodeParentID" "NodeLevel" and "NodeOrder" so you could transform any XML / JSON feed into a DataTable that had the dynamic 'page types' with hierarchy structure set so it could be transformed using Kentico's Hierarchy Transformations.

This is probably what you want to do. I would check out that custom code and do some Querying to see if you can pass it the DocumentID and a DateTime and get all the information joined together. Make sense?

0 votesVote for this answer Mark as a Correct answer

Binod Paikaray answered on May 8, 2017 17:09

Thanks very much Brenden and Trevor for your responses. Let me try these and come back with the good news :D

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.