Managing Attachments and Images Using Service Layer API

Objective

After completing this lesson, you will be able to manage attachments and images using Service Layer API

Supported Attachments

Before you can manage attachments and images in the Service Layer, you must create a network shared folder with read and write permissions on Windows. Then configure the network shared folder as the attachment folder in General Settings in the SAP Business One, Desktop client.

Note

For Service Layer running on Linux, you need to mount the Linux attachment folder to a Windows network shared folder. Please refer to the SAP note 3003664 on how to mount attachments to a shared folder.

Attachments and images can be managed in the Service Layer using the following entities:

Different entitles (Attachments2, Pictures, ItemImages, EmployeeImages) that are used to manage attachments and images in Service Layer.
  • Attachments2
  • Pictures
  • ItemImages
  • EmployeeImages

These have streaming capabilities and can be used to apply the Slug mechanism.

If you look at the Service Layer metadata, you’ll see that a streaming entity has a special attribute HasStream = "true", which means that the entity type is a media entity and represents a media stream, such as a photo.

Note

You can find more information about the MIME definition on the RFC Editor website.

Attachments2 Entity

The flowchart illustrates the Attachments2 system, where files are uploaded to a central Attachment collection.

This entity is an attachment collection, which can be linked to documents. The Attachment2 entity supports file types: .pdf, .doc, .docx, .jpg, .jpeg, .png, .txt, .xls, .ppt. It can handle an attachment size up to 50 MB.

General format for uploading attachments to a collection from a remote machine:

Code Snippet
1234567891011121314
POST /b1s/v2/Attachments2 HTTP/1.1 Content-Type: multipart/form-data;boundary=<Boundary> --<Boundary> Content-Disposition: form-data; name="files"; filename="<file1>" Content-Type: <content type of file1> <file1 content> --<Boundary> Content-Disposition: form-data; name="files"; filename="<file2>" Content-Type: <content type of file2> <file2 content> --<Boundary>--

General format for downloading attachments from a collection:

Code Snippet
1
GET /b1s/v2/Attachments2(<Attachmentid>)/$value?filename='<filenametodownload>'

Pictures

The image is a flow diagram illustrating the process of uploading and downloading pictures to Shared Picture location.

This entity allows you to upload and download images to the picture folder configured in the General Settings.

General format for uploading pictures:

Code Snippet
12345678910
POST /b1s/v2/Pictures HTTP/1.1 Content-Type: multipart/form-data;boundary=<Boundary> --<Boundary> Content-Disposition: form-data; name="files"; filename="<file1>" Content-Type: <content type of file1> <file1 content> --<Boundary>--

General format for downloading pictures:

Code Snippet
1
GET /b1s/v2/Pictures('<Pic_name.jpg>')/$value

ItemImages

The image depicts a data flow diagram for ItemImages uploads and downloads.

This entity allows you to manage the item image in Item master data.

General format for uploading an image to the Item master data:

Code Snippet
123456789
PATCH /b1s/v1/ItemImages(<ItemCode>) Content-Type: multipart/form-data;boundary=<Boundary> --<Boundary> Content-Disposition: form-data; name="files"; filename="<file>" Content-Type: <content type of file> <file content> --<Boundary>--

General format for downloading an image for the itemcode:

Code Snippet
1
GET b1s/v2/ItemImages(1)/$value

EmployeeImages

The image depicts a data flow diagram for EmployeeImages uploads and downloads.

This entity allows you to manage an employee image in the Employee master data.

General format for uploading an image to the Employee master data:

Code Snippet
123456789
PATCH /b1s/v2/EmployeeImages(<EmployeeCode>) Content-Type: multipart/form-data;boundary=<Boundary> --<Boundary> Content-Disposition: form-data; name="files"; filename="<file>" Content-Type: <content type of file> <file content> --<Boundary>--

General format for downloading an image for the Employee:

Code Snippet
1
GET b1s/v2/ItemImages(1)/$value

How to Upload, Download, and Update Attachments Using Service Layer API

You can link attachments to almost all documents in SAP Business One. To upload a file requires copying a physical file from one location to another, usually from a remote location to a central file server. The Service Layer API provides you with the resources to achieve this functionality.

In this video, you’ll learn how to handle attachments in SAP Business One using Service Layer, for example, how to upload, download, and update these files.

How to Manage Images in Service Layer API

In this video, you’ll learn how to manage images for Item master data in SAP Business One using Service Layer.

Upload Attachments

In this exercise, you’ll practice setting up the attachment folder and uploading an attachment to use in a sales order.

Before starting this exercise, make sure that you:

Steps

  1. Create a Windows shared path: \\win-srv\attachments.

    1. If you are using Service Layer on Linux complete the steps outlined in the SAP note 3003664.

  2. Login into the Demo UK database using SAP Business One Desktop Client.

  3. Go to menu → ModulesAdministrationSystem InitializationGeneral SettingsPath choose Attachments Folder on a Windows share, for example \\win-srv\attachments.

  4. To create the Attachment request using Service Layer API, amend the following HTTP sample request and send:

  5. Sample request

    Code Snippet
    1234567891011121314151617
    POST https://localhost:50000/b1s/v2/Attachments2 Content-Type: multipart/form-data; boundary=WebKitFormBoundaryUmZoXOtOBNCTLyxT --WebKitFormBoundaryUmZoXOtOBNCTLyxT Content-Disposition: form-data; name="files"; filename="line1.txt" Content-Type: text/plain Introduction B1 Service Layer (SL) is a new generation of extension API for consuming B1 objects and services via web service with high scalability and high availability. --WebKitFormBoundaryUmZoXOtOBNCTLyxT Content-Disposition: form-data; name="files"; filename="line2.txt" Content-Type: image/jpeg This is demo text for the learning. --WebKitFormBoundaryUmZoXOtOBNCTLyxT--

  6. Check the response:

    1. Check the response code - it should be 201 - Created if the HTTP request is successful.

    2. Check the response content - it should contain information on newly created attachments.

    3. Check the response header Location - it will contain the link to the newly created attachment collection.

    4. You can also verify whether the attachments are copied to the attachment folder which you have set in the General Setting.

  7. To update the sales order with the attachment, amend the following HTTP sample request and send:

  8. Sample request

    Code Snippet
    123456
    PATCH https://localhost:50000/b1s/v2/Orders(1229) { "AttachmentEntry": 5 }

  9. Check the response:

    1. Check the response code - it should be 204 - No Content Created if the HTTP request is successful.

    2. Check the response content - it should have a blank response.

    3. You can also verify whether the attachments are linked to the sales order in SAP Business One, Desktop client or Web client.

Log in to track your progress & complete quizzes