Exposing CDS views via XSOData
I was not planning on this post but I got a few questions from some colleagues as to how to expose CDS views via XSOData so here it is 🙂
As always, thank you for reading this post.
Before creating the xsodata service, let’s start by assuming you have created a CDS document with a table / view and you are ready to expose your data model. If you have not done those steps, please refer to my first CDS blog to create a CDS document and build a table / view
1) The first step is to create an .xsaccess file so we can expose our service. Make sure you set the property expose: true and your also need to set its authentication method. In my case, I do not want to add authentication as I am using HCPtrial, however, you may use null (for no authentication), Basic (for username and password), Form, etc. Please use the XS Admin tool for other authentication methods such as SAML, X509 certs, etc. check out the official documentation for xsaccess files
Also create an empty file .xsapp
2) create your .xsodata file (I am exposing the view I created on my blog 2) when exposing an end point on your xodata service, make sure your endpoint contains a KEY, otherwise, you may use a dynamically generated key as I did below
3) once you created your .xsodata file and have successfully activated it, run your service by clicking on the green icon with an arrow (see screen shot above). Your service will run on a separate tab on your browser and will display the XML of your service. One thing to note here is that my endpoint on the .xsodata service is exposed as “EVENTS” and that’s exactly the endpoint on my XSOData service on the browser (screenshot below)
4) your next step would be to explore the end point, and the data that will be returned, etc. I am not able to run my service due to insufficient privileges since i am running this service on my HCP trial account, but if you are able to activate your service and you can query it from your browser, then you should be able to see the records being returned by your odata service.
5) for a JSON formatted response, please include $format=json at the end of your service URL.
you can also perform other query operators by appending query string paramaters to the end of your URL endpoint.
here are a few:
$top=1 for the first record
$filter=COLUMN eq ‘value for filtering results where COLUMN equals ‘value’
$format=json for your JSON formatted response
$orderby=COL_A for ordering the response by COL_A
for more OData operations and syntax, please visit the odata official documentation
thanks again and hope this has been helpful for others. As always, please share your experiences and struggles and feel free to ask questions.