Skip to Content
Author's profile photo Former Member

Uploading file in HANA cloud Repository using DMS in XS engine

Uploading file in HANA cloud Repository using Document management service
in XS engine



Introduction to document services


Many applications need to store and retrieve unstructured content. Traditionally, a file system is used for this purpose. In a cloud environment, however, the usage of file systems is restricted. File systems are tied to individual virtual machines, but a Web application often runs distributed across several instances in a cluster. File systems also have limited capacity.


The document service offers persistent storage for content and provides additional functionality. The document service provides a standardized interface for content using the OASIS CMIS standard.


The following figure illustrates the document service’s architecture:





Implementing Document Services


This blog is mainly based on detailed steps of uploading a file to HANA cloud repository using document management service in XS engine.


In this blog we have created a simple UI5 (html) page which is used to upload files from presentation server.  We are using XSJS service to upload the document onto document repository in HANA cloud. The XSJS service uses standard document management services to upload the document onto repository.




  1. We have used HANA cloud trial account to create and test DMS service.
  2. DMS service used in this example is currently only available in trial version.




Before uploading document to DMS you need to create a repository or use an existing repository in HANA cloud account. Here in this example we create the following repository:



Login to HANA cloud account and go to Document Repository tab:



Create a new Repository (You may also use an existing repository):





I have created the following files to use and test the Document management service:

  1. .xsaccess:
    This is needed to expose the files.
  2. .xsprivileges:
    This is needed to specify application privileges.
  3. Ecm.xshttpdest:
    This is needed to specify cloud instance details.
  4. Index.html
    To test the service. This file creates a simple UI where user can
    upload any document. This page passes the document to XS engine for further processing
    (uploading the document in HANA cloud repository).
  5. Fileupload.xsjs:This is an XSJS script which creates a DMS session and uploads the binary
    contents of the file to HANA cloud repository.

Details of each of these files are as follows:

  • .xsaccess

First you need to create .xsaccess file which will expose your project with basic authentication.


“exposed” : true,
“default_file”: “index.html”



  • .xsprivileges

Next you need to define privileges which should define the type of access users will have to the services created. Specify privilege  as Basic for this test.


{ “privileges” :


[ { “name” : “Basic”, “description” : “Basic usage privilege” } ]



  •     Ecm.xshttpdest:

This file shall provide HANA cloud host details. This file is used in XSJS service to connect to HANA cloud system and access the repository using Document management services.


  1. To create a session you have to provide a destination containing the repository URL first.
  2. Store the following text in a file named Ecm.xshttpdest and replace the host value placeholder <account> with the name of your account.



description = “Document Service”;

host = “ecm-<account>”;

port = 8443; (Replace this with port name if it’s different from 8443)

pathPrefix = “/s/json”;

useProxy = false;

authType = none;

useSSL = true;

timeout = 300;



  • Index.html

Next you need an HTML form, to test document management service. Here user will have an option to upload a file which shall be passed to XS engine.


<!DOCTYPE html>



<title>File Upload Example</title>



<h4>Upload a File to document service</h4>

<form action=”fileupload.xsjs” enctype=”multipart⁄form-data” method=”post”>


<input type=”text” name=”description” size=”30″><br/>


<input type=”file” name=”datafile” size=”40″><br/>

<input type=”submit” value=”Submit”><br/>





This form submits a POST request to fileupload.xsjs. There the multipart request has to be parsed.


  • fileupload.xsjs

Next you need to create xsjs file, to parse file uploaded by the user and upload it to HANA cloud repository.

During execution this file executes the following steps:

  1. Parse the uploaded document
  2. It connects to a repository.
  3. It creates a document.


Please change the repository name and repository key in the following sample code:


// Extract the content from the multipart entity and return a CMIS content object
// This function reads the uploaded file content and returns the details to the calling service

function getCmisContentObject(entity) {
var content = {};

if (entity) {
var j; = entity.body.asArrayBuffer();
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “~content_filename”) {
content.filename = entity.headers[j].value;
} else if (entity.headers[j].name === “content-type”) {
content.mimeType = entity.headers[j].value;
if (!content.filename || content.filename.length === 0) {
content.filename = “data.bin”;
if (!content.mimeType || content.mimeType.length === 0) {
content.mimeType = “application/octet-stream”;
return content;


// get the mulitpart content entity with given name

function getContentEntity(fieldName) {
var i;
var entity = null;

if ($.request.entities) {
for (i = 0; i < $.request.entities.length; ++i) {
var j;
entity = $.request.entities[i];
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “content_name” &&
entity.headers[j].value === encodeURIComponent(fieldName)) {
return entity.body.asArrayBuffer();
return entity;

function getStringEntity(fieldName) {
var i;
var entity;

if ($.request.entities) {
for (i = 0; i < $.request.entities.length; ++i) {
var j;
entity = $.request.entities[i];
for (j = 0; j < entity.headers.length; j++) {
if (entity.headers[j].name === “content-disposition” &&
entity.headers[j].value === “form-data; name=\”” +
encodeURIComponent(fieldName) + “\””)  {
return entity.body.asString();
return null;

if($.request.method === $.net.http.POST) {
var name;
var mimeType;
var content;
var customProperty;
var description;
var contentIndex = -1;
var i;
var j;
var id;
var body;
var headers;
var message = “”;
var records = [];

var entity = getContentEntity(“datafile”);
// Get document details
content = getCmisContentObject(entity);
// Get document description
description = getStringEntity(“description”);

if (content) {
try {
// Create Document repository session
var session = docService.cmis.createHANAXSECMSession({
destination : $.net.http.readDestination(“acme.cmistestapp.cmisjs”, “ecm”)
}, {
repositoryName : “XXXXXXXX”, // Provide repository name which was created earlier.
repositoryKey : “XXXXXXXXX” // Provide repository key which was set earlier.
// Create a new document in Document repository
session.init().then(function(repInfo) {
session.getRootFolder().then(function(rootFolder) {
“cmis:name” : content.filename,
“cmis:description” : description,
“cmis:objectTypeId” : “cmis:document”
}, content).then(function(newDoc) {
id = newDoc.getId();
message += “Your file was archived with:\n”;
message += “name: ” + content.filename + “\n”;
message += “mime type: ” + content.mimeType + “\n”;
message += “id: ” + id + “\n”;
} catch (e) {
message += “Error ” + + “: ” + error.message + “\n”;
} else {
message += “No content found in request.\n”;

message : message,
id      : id

// send response
$.response.contentType = “application/json”;
$.response.status = $.net.http.OK;
} else {
message = “Unexpected http method called.”;
// unsupported method
var records = [];

message : message,
id      : id

$.response.contentType = “application/json”;
$.response.status = $.net.http.OK;




Create a folder


We have not created a new folder in document repository in the above example. You can use the following code if you want to create a new folder inside document repository.


A folder is created as child of another folder. Each repository by default has a root folder. The root folder can be retrieved from the session. Names must be unique within a folder.


var rootFolder = var rootFolder = session.getRootFolder();

“cmis:name” : “newFolder”,
“cmis:objectTypeId” : “cmis:folder”
}).then(function(newFolder) {
var folderId = newFolder.getId();

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Amit,

      Can you help me in understanding the below line of code.

        destination : $.net.http.readDestination("acme.cmistestapp.cmisjs", "ecm")

      I am getting Error 405



      Author's profile photo Former Member
      Former Member

      I get the same error
      Will you have any solution?

      Author's profile photo Sergio Guerrero
      Sergio Guerrero

      the readDestination methos is part of the outbound connectivity api - documentation is below$.net.http.html

      hope this helps

      Author's profile photo Former Member
      Former Member

      Hi Sergio Guerrero,

      Now my problem is library sap.bc.cmis

      var docService =  $.import("sap.bc.cmis", "cmis"); //Here I have problems with this library. Or should I use another?
      var session = docService.cmis.createHANAXSECMSession({  code ...


      Author's profile photo Sergio Guerrero
      Sergio Guerrero

      I do not see how Amit declared / got the docService variable either.. i am in the middle of testing this as well but noticed the missing instantiation of that variable.

      maybe Amit can let us know where that came from

      Author's profile photo Former Member
      Former Member

      Return error:

      [{"message":"Error ReferenceError: docService is not defined\n"}]
      I do not know how I can refer to variable docService

      Is there another way to use document service to upload my files?


      Author's profile photo Former Member
      Former Member

      Hi ,

      I am getting an error while running index.html as

      404 - Not found

      We could not find the resource you're trying to access.
      It might be misspelled or currently unavailable.

      Please help me out.

      Author's profile photo Former Member
      Former Member


      Seidor Hcp...Are you able to resolve this issue of docService Variable. If yes please suggest as I am also stuck at that point.


      Author's profile photo Former Member
      Former Member

      Not is possible, i applied java for connect to document service.

      Author's profile photo Ivan Mirisola
      Ivan Mirisola

      Please ignore the instructions from this blog and use the Java Proxy Bridge to communicate with the Document Service.

      Author's profile photo MATEEN MOMIN

      hi, after following the instructions at help and this detailed youtube video about SAP HANA Academy - Java Development on HCP: Document Service and Unstructured Content 

      i am getting authorization error. Any idea why?

      HTTP Status 403 - Access to the requested resource has been denied

      Author's profile photo Ivan Mirisola
      Ivan Mirisola

      Could you please check the http traces from F12? Try to identify where that 403 came from.

      You need to figure out which http request is being denied by which server first.