Sense/Net OData tips and tricks

At Sense/Net we pride ourselves on working with the cutting edge. Thus we implemented a REST API for our Content Repository which is based on the OData standard by Microsoft. This means that without any extra effort, our software automatically becomes compatible with a huge bunch of already existing software which can utilize our OData service as clients.

To maximize user experience, we've created a completely custom OData implementation. It currently supports most of the standard with a small amount of custom extensions and a few features left out. But don't worry, this is just the first iteration of the feature, stay tuned for more. Also, do check out Tamás's blog post about the API itself and my own blog post about the general advantages of having a REST API.

Today I’m here to show you some examples and tricks about using this API to the fullest.

For now, let's stay in the browser and use AJAX for accessing them. There are a bunch of built-in articles in the demo site too, so let's just use those for now. (So that anyone can try out these even on our demo site.)

As a first example, let's say you want to access some content in your Sense/Net installation. Here's how you do that with jQuery.

$.ajax({
    type: "GET",
    url: " /OData.svc/Root/Sites/Default_Site/NewsDemo/Internal",
    dataType: "json",
    success: function (result) {
        // Do something with the result
    },
    error: function () {
        alert("Error!");
    }
});

 

Note that the resulting thingy in the success callback is a JavaScript object. This means that you can treat what you get back from the API in an object-oriented manner in JavaScript. It's very powerful.

You can see that the result contains way too much data for us, so let's use the $select query parameter to tell the Repository what we actually want from it. Let's get the Id, Path, and Lead text of the articles.

$.ajax({
    type: "GET",
    url: "/OData.svc/Root/Sites/Default_Site/NewsDemo/Internal?$select=Id,Path,Lead",
    dataType: "json",
    success: function (result) {
        // Do something with the result
    },
    error: function () {
        alert("Error!");
    }
});

Let's do something useful with the resulting stuff now. We're using Kendo UI ourselves these days, and so should you. Let's create a simple template using the template framework of Kendo UI and render our results with that.

Here's a rudimentary template which you can use to render the results:

<script id="javascriptTemplate" type="text/x-kendo-template">
    <ul>
    # for (var i = 0; i < data.length; i++) { #
        <li>#= data[i].Lead #</li>
    # } #
    <ul>
<script>

 

This basically lays out the articles in a simple way and provides links for viewing them in full. Next step: let's also retrieve the last modifier user of the articles. Here's how you would do that:

/OData.svc/Root/Sites/Default_Site/NewsDemo/Internal?$select=Id,Path,Lead,ModifiedBy

 

Oops! We can see that the resulting object has a __deferred property in it. This means that the object is another entity and is not fully loaded right now because that would cause too much data to be transferred through the wire. This is when the $expand query parameter comes to the rescue! (Note: you may have to upgrade your Sense/Net 6.2 installation to the latest release get this feature.)

Here's the URL to get what we want:

/OData.svc/Root/Sites/Default_Site/NewsDemo/Internal?$select=Id,Path,Lead,ModifiedBy&$expand=ModifiedBy
 

More tutorials are on the way to help you guys utilize this awesomeness.
Stay tuned for more OData goodness!

 

 

Comments are closed

Welcome to the blog!

Sense/Net ECM is ever evolving. Community means the world to us so we want to keep you apprised on what’s happening on our side of the woods. Want to make us happy? Add a comment and tell us what you think!

Month List