icon Web API
Created by Orckestra

Example 2: Getting data using OData

How to get data using OData

In the following example, we'll use standard C1 pages as data and build a Web API that will serve as a web point to retrieve this data. Then we'll create a C1 Razor function that will use OData to get the data.

Creating a Web API

  1. Make sure you've installed the Composite.AspNet.WebAPI package.
  2. Add a new class called "ODataTest" in /App_Code on your website.
  3. Replace the generated code with this code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    using System.Linq;
    using System.Web.Http;
    using System.Web.Http.OData.Query;
    using Composite.Data;
    using Composite.Data.Types;
      
    namespace Composite.Controllers
    {
        public class PagesController : ApiController
        {
            [Queryable]
            public IQueryable<IPage> Get(ODataQueryOptions<IPage> options)
            {
                using (var c = new DataConnection())
                {
                    var querySettings = new ODataQuerySettings();
      
                    return options.ApplyTo(c.Get<IPage>(), querySettings).Cast<IPage>().ToList().AsQueryable();
               }
            }
        }
    }
    Download the sample code

Alternatively, you can build it as a class library and add it to your website:

  1. Create a Class Library project in Visual Studio.
  2. Add required references to it.
  3. Add a class called "ODataTest" to it.
  4. Replace the generated code with the code above.
  5. Build the project.
  6. Copy the DLL to /Bin on your website.

Creating a C1 Razor function for data retrieval

  1. Log in to the C1 Console.
  2. In the Functions perspective, add a new Razor function called "ODataTest".
  3. Replace the generated code with this code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    @inherits RazorFunction
     
    @functions {
        public override string FunctionDescription
        {
            get  { return "A demo function that outputs a hello message."; }
        }
    }
     
        <head>
            <script type="text/javascript">
    // <!--           
                function LoadJson(url)
                {
                    $.getJSON(url, function (data) {
     
                        $("#jsonResult")[0].textContent = JSON.stringify(data, null, 4);
                    });
                }
    //-->
            </script>
            <style type="text/css">
                .queryLink {
                    display: inline-block;
                    background-color: lightblue;
                    padding: 10px 20px;
                }
            </style>
        </head>
        <body>
             
            <a href="#" onclick="LoadJson('/api/pages?$top=1')" class="queryLink">
                Load a page
            </a>
     
            <a href="#" onclick="LoadJson('/api/pages')" class="queryLink">
                Load all pages
            </a>
             
             
            <a href="#" onclick="LoadJson('/api/pages?$top=5')" class="queryLink">
                Load top 5 pages
            </a>
             
            <a href="#" onclick="LoadJson('/api/pages?$filter=Title eq \'OData\'')" class="queryLink">
                Filter by Title='OData'
            </a>
             
            <pre id="jsonResult" style="padding: 10px; max-width: 700px; border: 2px solid black">
                 
            </pre>
             
             
        </body>
    </html>
    Download the sample code
  4. Edit or create a page.
  5. Insert this function on the page.
  6. Save and publish the page.

Testing the demo

Now you can get data (C1 pages) in one of the following ways:

  • Making requests via the URL:
1
2
3
http://<websiteurl>/api/pages?$top=2
http://<websiteurl>/api/pages?$orderby=Title

  • Clicking the respective buttons on the page where you inserted the "ODataTest" function.
Back to top