Skip to Content
Author's profile photo Sergio Guerrero

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

/wp-content/uploads/2016/02/1_897005.jpg

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

/wp-content/uploads/2016/02/2_897006.jpg

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)

/wp-content/uploads/2016/02/3_897007.jpg

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.

/wp-content/uploads/2016/02/4_897008.jpg

5) for a JSON formatted response, please include $format=json at the end of your service URL.

a). URL_HOST/path/to/service/ENDPOINT?$format=json

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.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sergio Guerrero
      Sergio Guerrero
      Blog Post Author

      quick update: I was able to figure out how to run the xsodata service and return data successful - sorry it took me a while to update this blog -

      we have to create an hdbrole and assign the sql objects to it we need to have access to and also the type of privilege (select / insert/ delete ). There is only one issue with the hdbrole on HCP  trial .. the graphical editor seems like it has an issue so the hdbrole I created was via the text editor.

      I created a role as follows:

      role path.name.to.myrole {

         sql object <name of my table/or view> : SELECT;

      }

      in here i showed only one, but there may be more than 1 rows in the role.  Once the role is activated, then go to the sql console and grant the role to a user, in my case, I followed this post

      which showed me how to do ..

      call "HCP"."HCP_GRANT_ROLE_TO_USER"('<ROLE_NAME>','<P_USER_ID>')



      hope this is helpful for others!