We have some XML data that's stored as a content item into the database via Tridion. I need to be able to access the data for a jquery datatable. Is there an API in SDL Tridion 2011 SP1 that would allow me to access the stored data the way I would access a physical file? Something like an ajax script? Hoping it will be easier to go through a Tridion API than going direct to the database.
2 Answers
Look at the Content Delivery Web Service, it is a REST endpoint based on oData, which allows you to query Component Presentations, Pages and more from the Broker DB via web service calls. CD Web Service API Documentation
If your DCPs or Pages contain XML, then you can retrieve that and parse it or load it in your jquery datatable.
Here is an example query which you could call from jquery AJAX: oData REST Query
Just found a basic example in my archive, this is for HTML DCPs, so you would have to parse the Xml instead of just displaying the HTML. You could also consider publishing content as JSON. As Bart mentioned, latest versions of SDL Web and DXA would make this implementation so much easier.
<div>
<script type="text/javascript" src="/system/includes/jquery.min.js"> </script>
<script type="text/javascript" src="/system/includes/jquery.custom.min.js"></script>
<script type="text/javascript" src="/system/includes/json2.min.js"></script>
<h1>Top 3 Products:</h1><br />
<script type="text/javascript">
// Build OData query
var query = "http://preview.website.com/cd_webservice/odata.svc"
+ "/ComponentPresentations()?$filter=(PublicationId eq 3) and (TemplateId eq 6040)&$top=3&$select=PresentationContent"
+ "&$format=json"; // json request
$.ajax({
async: true,
type: "GET",
url: query,
contentType: "application/json;charset=utf-8",
data: null,
dataType: "json",
success: function (res, status) {
callback(res);
}
});
// Display the results
function callback(result) {
var html = "";
result = result["d"]["results"];
for (var i = 0; i < result.length; i++) {
html += result[i].PresentationContent;
//here is your XML if your Component Presentation output is in XML format
}
$("#odata").html(html);
$("#odata .accordion").accordion();
}
</script>
<div id="odata" style="height:auto;width:500px;">
</div>
If you need this on the Content Manager side, consider using the Core Service API (.Net WCF Webservice with Java endpoint as well): Core Service Documentation
There is no JavaScript or JQuery API available for the Content Delivery side, you can access this server side with either Java or .NET using the documented APIs, read all about it here.
Going directly to the database is also not advised, since the data model of the content delivery is not open, meaning that its data structure can change without notification on every new release. So if you were to use a Broker database connection, your solution might not be compatible with future upgrades.
Maybe this is also a good time to mention that the version you tagged this question on is very old, and it could be wise to consider upgrading to SDL Web 8, which is the latest release. That still won't give a more satisfying answer to your question, but will at least bring you on a version which isn't retired.