Technical Articles
Create a pdf attachment using the Attachments API in S/4HANA Cloud
Welcome to this short blog post showing you everything you need to add an attachment to an object in S/4HANA Cloud by using the Attachments API and Postman. I wanted to write this blog post as I couldn’t find an easy to follow blog post on how to use this API to create a pdf attachment for S/4HANA Cloud.
This blog post assumes that you have already set up the necessary communication arrangement(s) and that you have access to the API with a technical user. If you need help setting up communication arrangements, you can go here. If you don’t know which communication scenario you need to activate, please refer to the Attachments API link above.
Walkthrough
Open up Postman and create a new HTTP request, then fill out the request URL and Authorization (will likely be Basic Authentication for Username/Password).
Navigate to the Headers tab and create the following headers:
- x-csrf-token, for those that do not know, you will first need to do a HEAD or GET request with the value “fetch” in order to retrieve a token to be used in the POST request when we attach the file.
- slug, this will be the name of the file saved in S4HC
- Content, this is the file location (Note: you should be able to execute the request with these headers and no Body, however if it doesn’t work for you, please provide a Body as explained later)
- Content-Type, the file type of the file
- BusinessObjectTypeName, this is the ID of the object type you want to attach to. My example is for a Sales Order which is value BUS2000116
- LinkedSAPObjectKey, this is the key or number of the object you want to attach to. My example is for a Sales Order number 8000099999
You are almost ready to attach a file through the API!
Navigate to the Body tab, and select the radio button for “binary”. From here you can select the file you want to upload.
Everything is now in place to attach the file through the API. Now follow these steps
- set the value of the x-csrf-token header to fetch
- execute a HEAD or GET request to retrieve an x-csrf-token
- once you have this token, copy and paste it’s value over the fetch value in the x-csrf-token request header
- change the HTTP request to POST now and execute the request
You should see the following successful message:
If you get an error message, then please check the SAP community or comment here and I can help if I can.
If it is successful for you, then navigate to the object and try open the file to ensure it all worked as expected!
I hope you found this bog post useful.
If you have any thoughts, ideas, or feedback, then I encourage you to comment below!
Additionally, if you would like to do further reading around this topic, then please use these great resources: SAP S/4HANA Community page, SAP Q&A page, other SAP blogs.
If you would like similar content, then follow my profile where I will be publishing similar blog posts for easy-to-follow technical insights.
This API can attach several different types of objects but these need to be done in a different way.
Please comment and let me know if you'd like to see blog posts on more of the Attachments API functionality.
Hi David,
thanks for your blog post, it was very well written and easy to follow along! Unfortunately, the request doesn't seem to work for me and I'm getting the following error: "You have no authorization to execute action Edit for the document".
I can't seem to understand the reason for the error, as my communication user has been setup to allow for a Sales order integration.
Have attached 2 screenshots showing my request body + error message, as well as the setup of my communication arrangement. Would appreciate and guidance you can offer on this issue!
Thanks and best regards
Varun
In case anyone else runs into a similar error, here is the solution that worked for me.
In the headers tab, use 'BUS2032' as the value for the key 'BusinessObjectTypeName' instead of 'BUS2000116'. This seemed to do the trick for me, hope it helps!
Thank you Varun for sharing your experiences!
Hello David
A very nice and simple blog to solve the attachment problem exactly I was looking for.
Could you help me how to find the BusinessObjectTypeName for any business object? I am looking the type for Billing Document and Billing Document Request.
Regards
Babu
Hi Babu,
The way I got around this was to create a Custom Analytical Query on the CDS views that holds this data. I found it was case-sensitive so bear that in mind when searching.
Kind regards,
David
Hello David,
I am calling get attachment but getting error saml2=disabled
any idea why?
thanks
Hi,
What type of authentication are you using?
Some screenshot or error text may help us find the correct solution if you can share those too.
Kind regards,
David