cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Cloud Integration: Create Attachment in S/4 via CPI and API_CV_ATTACHMENT_SRV

Christopher
Participant

creatingpdffrombase64script.txtHello there,

there are several (open) questions regarding the use of the API Attachment Service API_CV_ATTACHMENT_SRV. We are able to create attachments in S/4, but we can't show the attachment. SAP's response in a ticket to this is:

"Being Attachment Service , we are storing the content provided by the user and when it is downloaded back then we will give the content in same format as the input. So basically if user provides "XYZ" then we will return "XYZ" only, and attachment service really not validate or understand the input form of the content."

Now, to enable the attachment service to directly display the file, SAP provided a code snippet that can be used to achieve this. The problem is, the snippet is in JavaScript and the JavaScript environment in the CPI does not include the used libraries. Do you have any idea how to get the code snippet running in the CPI environment?

	onCreatePdf: function () {
			
			/* To Create a file using the BASE 64 content using the API_CV_ATTACHMENT_SRV */
			var fileName = "fileFromBase64.pdf";
			// BASE 64 String of the file
			var base64_str = 
				"JVBERi0xLjQNCiWm6c/EDQoxIDAgb2JqDQo8PA0KL0NyZWF0b3IgPEZFRkYwMDU0MDA2NTAwNzMwMDc0MDAyRTAwNzQwMDc4MDA3NDAwMjAwMDJEMDAyMDIG4NCjAwMDAwMjQ0MjMgMDAwMDAgbg0KdHJhaWxlcg0KPDwNCi9TaXplIDExDQovSW5mbyAxIDAgUg0KL1Jvb3QgMiAwIFINCi9JRFs8YjRmYTIxYjdmNDkwYTRlMDdkYjZjNzVmOGZkZDY5NTA+PGI0ZmEyMWI3ZjQ5MGE0ZTA3ZGI2Yzc1ZjhmZGQ2OTUwPl0KPj4NCnN0YXJ0eHJlZg0KMjQ1ODcNCiUlRU9GDQo=";
			// decode the BASE 64 string
			var raw_file =atob(base64_str);
			var n = raw_file.length;
			/*global Uint8Array*/
			/*eslint no-undef: "error"*/
			var u8arr = new Uint8Array(n);
			while(n>=0)
			{
				u8arr[n] = raw_file.charCodeAt(n--);
			}
			var file ;
			var properties = {type: 'application/pdf'}; // Specify the file's mime-type.
			try {
			  // using charcter array create file object for the given string
			  file = new File([u8arr], fileName, properties);
			} catch (e) {
			   // do nothing or return
			}
			// Once the file got creating then get the CSRF token and after that make furhter ajax call for creating attachment 
			$.ajax({
			    url: '/sap/opu/odata/sap/API_CV_ATTACHMENT_SRV',
			    type: 'GET',
			    async: false,
			    cache: false,
			    headers: { "x-csrf-token": "fetch" },
			    complete: function (getResponse){
			    	// Read the CSRF token from response headers 
			    	var respHeaders = getResponse.getAllResponseHeaders();
					respHeaders = respHeaders.split(/\n|\r|\r\n/g).reduce(function(a, b) {
					    if (b.length) {
					        var [ key, value ] = b.split(': ');
					        a[key] = value;
					    }
					    return a;
					}, {});
					var csrfToken = respHeaders["x-csrf-token"];
					// make further ajax call to API_CV_ATTACHMENT_SRV to create attachment
					$.ajax({
					    url: '/sap/opu/odata/sap/API_CV_ATTACHMENT_SRV/AttachmentContentSet',
					    type: 'POST',
					    data: file,
					    async: false,
					    headers: {
					    	//  content type as per the type of file uploaded as base 64
							"Content-Type": "application/pdf",
							"slug": fileName,
							"BusinessObjectTypeName": "MARA",  
							"LinkedSAPObjectKey": "M02",
							"x-csrf-token": csrfToken
						},
					    success: function(res) {
					        // on succesfull you will get 201 response code and the same is verified in developer tools
					    },
					    cache: false,
					    processData: false
					});
			    }
			});
		}

Accepted Solutions (0)

Answers (0)