third party database content lessons/examples

jim.lorenz's Avatar


24 Aug, 2015 03:10 PM

I need to include dynamic content from an external database. What's the recommended approach and are there any lessons/examples?

  1. 1 Posted by jim.lorenz on 24 Aug, 2015 06:30 PM

    jim.lorenz's Avatar

    ok, I've found some other discussions but none of them are current. Plus the references to web services and feeds seem way to complicated. Embedded code seems like most straight forward ( However, what's the current recommended practice? Any complete examples?

  2. Support Staff 2 Posted by Tim on 25 Aug, 2015 03:13 PM

    Tim's Avatar

    Hi Jim,

    There are a few different ways you could potentially do this (as you noticed), but if you are just looking to grab small bits of content from your external database then I agree that you can look to embed code within the content of your pages. Doing so will require that you make use of passthrough code sections to surround your code snippets. The end result will be that the external content will not be displayed within Cascade, but will be displayed once published to your .NET web server(s).

    For example, your code snippet might end up looking something like this:

    (your .NET code goes here)

    As far as best practices are concerned, it's generally best to include these types of code snippets within Blocks and/or Formats as opposed to entering directly into a WYSIWYG editor. The reason for that is that since most end users don't have the ability to edit the HTML source directly, they could unintentionally delete this code without ever knowing it was there (since the passthrough code sections are essentially HTML comments that won't appear in the rendering).

    If you do go the .NET route, you'll also need to add your .NET header to your Template(s) using the passthrough-top code sections.

    I looked around for some examples of this but wasn't able to find anything. I'm sure other folks have done something similar so perhaps they can chime in here, but in the meantime feel free to comment back with any followup questions and I'll do what I can to provide more recommendations.


  3. 3 Posted by jim.lorenz on 25 Aug, 2015 09:26 PM

    jim.lorenz's Avatar

    Hi Tim,

    Thanks so much for your attention. It’s too bad there isn’t a way to write a custom index which accessed the external db and returned an xml result set.



  4. Support Staff 4 Posted by Tim on 25 Aug, 2015 09:30 PM

    Tim's Avatar

    Hey Jim,

    No problem.

    It’s too bad there isn’t a way to write a custom index which accessed the external db and returned an xml result set.

    Does your external database have an option to produce an XML file that you can place on a server somewhere? If so, you could easily do the following:

    • Create a new Feed Block and just provide it with the URL to your XML file (that was generated from the external database content)
    • Attach this Feed Block to any Template, Configuration Set, or Page region as needed
    • Optionally write a XSLT or Velocity Format to transform that XML file into XHTML elements for displaying on a web page

    Let me know if this helps! This is a very common method of consuming external content and working with it inside the system.

  5. 5 Posted by Bryce Roberts on 26 Aug, 2015 12:38 PM

    Bryce Roberts's Avatar

    You can write either custom XSLT with an extension or Velocity format (reflection) to return to the transform context an JDOM object. You can directly output this into the page and transform again with a page level XSLT, or you can continue to process the object.

  6. 6 Posted by jim.lorenz on 26 Aug, 2015 02:16 PM

    jim.lorenz's Avatar

    This sounds interesting. However, as a Cascade “newbe” I don’t understand. I am writing my own XSLT at the page level and I can code c# aspx. I’d really appreciate seeing an example or even a diagram of your approach would help.



  7. 7 Posted by Roberts, Bryce ... on 27 Aug, 2015 02:12 AM

    Roberts, Bryce Leitner's Avatar


    Some example code:

    <xsl:stylesheet xmlns:xsl="" version="1.0" xmlns:db="" xmlns:xalan="">
        <xsl:output indent="yes"/>
        <!-- Template for Selecting the root folder -->
        <xsl:variable name="connectStr" select="string(//sql/connect)"/>
        <xsl:variable name="userStr" select="string(//sql/user)"/>
        <xsl:variable name="passwordStr" select="string(//sql/password)"/>
        <xsl:variable name="sqlStatementStr" select="string(//sql/statement)"/>
        <xsl:variable name="dbConn" select="db:getConn($connectStr, $userStr, $passwordStr)"/>
        <xsl:variable name="resultXml" select="db:getResult($sqlStatementStr, $dbConn)"/>
        <xalan:component functions="getConn,getResult" prefix="db">
            <xalan:script lang="javascript"><![CDATA[
                var dbConnection, dbStatement, dbResultSet;
                function getResult(statment, dbConn){
                    if(statment && dbConn){
                        returnDoc = createDoc();
                        try {
                            var statements = dbConn.createStatement();
                            var resultSet = statements.executeQuery(statment);
                            getResultSetMetaData(resultSet, returnDoc);
                            getResultSet(resultSet, returnDoc);
                            return returnDoc;
                        } catch(e) {
                            return returnDoc.appendChild(returnDoc.createTextNode(e));
                function getResultSet(rs, DOMDoc){
                    resultSetEl = DOMDoc.createElement("resultset");
                    while ( {
                        result = DOMDoc.createElement("result");
                        for (var i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                            colName = rs.getMetaData().getColumnName(i);
                            column = DOMDoc.createElement(colName);
                            o = rs.getObject(i);
                            if(o != null){
                            } else {
                function getResultSetMetaData(rs, DOMDoc){
                    md = rs.getMetaData();
                    dbResultSetColumnName = [];
                    meta = DOMDoc.createElement("meta");
                    columnCount = md.getColumnCount();
                    columnCountEl = DOMDoc.createElement("columncount");
                    for(var i = 1; i <= columnCount; i++){
                        var columnEl = DOMDoc.createElement("column");
                        var columnNameEl = DOMDoc.createElement("name");
                        var columnTypeEl = DOMDoc.createElement("type");
                        var columnPrecisionEl =  DOMDoc.createElement("precision");
                function createDoc() {
                    docFactory = new Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance();
                    docBuilder = docFactory.newDocumentBuilder();
                    doc = docBuilder.newDocument();
                    rootElement = doc.createElement("db");
                    return doc;
                function getConn(connStr, userStr, passwordStr){
                        var dbConnection = new, userStr, passwordStr);
                        return dbConnection;
                    } catch(e) {
                        return e;
                function cleanUp(){
                    dbConnection = null;
                    dbStatement = null;
                    dbResultset = null;
                var driverSet = ["sun.jdbc.odbc.JdbcOdbcDriver", "net.sourceforge.jtds.jdbc.Driver", "com.mysql.jdbc.Driver", "oracle.jdbc.driver.OracleDriver"];
                function isDriverLoaded(driverClass){
                    var currentDrivers = getDrivers();
                    for(var i = 0; i < currentDrivers.length ; i++){
                        if(currentDrivers[i] == driverClass){
                            return true;
                    return false;
                function getDrivers() {
                    var drivers = [];
                    var getSetDriverManager = new;
                    for (e = getSetDriverManager ; e.hasMoreElements() ;){
                    return drivers;
                function getDriversList(){
                    return getDrivers().join(' ,');
                function loadDrivers() {
                    for(var i = 0; i < driverSet.length ; i++){
                    } catch(e) {return e.toString();}
                    return getDrivers();
                function unLoadDrivers() {
                    /*Once unloaded the driver is no longer able to be dynamically reloaded because the package announcement is only one time */
                    return getDrivers();
                function SQLite() {
                    } catch(e) {}
                    this.connection = null;
                    this.connect = function(sDBFileName) {
                        try {
                            this.connection = new'jdbc:sqlite:' + sDBFileName);
                            this.statement = this.connection.createStatement();
                        } catch(e) {
                            this.connection = null;
                            this.statement = null;
                        return null;
                    this.close = function() {
                        if (!this.connection) return;
                        this.connection = null;
                        this.statement = null;
                        return null;
                    this.query = function(sQuery) {
                        if (!this.statement) return;
                        var aResultSet = [];
                        try {
                            var oResultSet = this.statement.executeQuery(sQuery);
                        } catch (e) {}
                        if (oResultSet) {
                            while ( {
                                var aRowData = {};
                                for (var c = 1; c <= oResultSet.columnCount; c++) {
                                var sColumnName = oResultSet.getColumnName(c);
                                aRowData[sColumnName] = oResultSet.getString(sColumnName);}
                             return aResultSet;
                function getSQLite(){
                    var returnObj;
                    try {
                        returnObj = new SQLite();
                    } catch(e) {
                        returnObj = e;
                    return returnObj;
  8. 8 Posted by jim.lorenz on 27 Aug, 2015 12:15 PM

    jim.lorenz's Avatar

    Thanks guys – I’ll study, and hopefully learn from, this.



  9. Support Staff 9 Posted by Tim on 27 Aug, 2015 02:42 PM

    Tim's Avatar

    Thanks for chiming in and providing that example, Bryce!

    Jim, let us know how things go and certainly reach out here again if you come up with more questions. Have a good one!

  10. Tim closed this discussion on 27 Aug, 2015 02:42 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac