top of page
Search

Archivelink with Python and ABAP

Updated: Oct 13


ree

SAP ArchiveLink with Python and ABAP

This post demonstrates how to use SAP ArchiveLink through practical examples in both ABAP and Python.With some creativity, these code samples can serve as building blocks for creating a lightweight content-management interface that interacts directly with ArchiveLink.


What Is SAP ArchiveLink?

SAP ArchiveLink connects SAP business objects to external storage systems, enabling secure and compliant document management within the SAP environment.

Key Capabilities

  • Document Linking – Attach external files (PDFs, images, scans) to SAP objects such as invoices, purchase orders, or HR records.

  • Document Management – Store, retrieve, and display documents across multiple repositories.

  • Process Integration – Trigger workflows automatically when documents are archived or retrieved.

  • Compliance and Security – Ensure encryption, retention control, and secure document access.

  • HTTP Content Server Support – Enable standard web-protocol integration.

  • Scanning and Archiving – Digitize paper-based records directly into SAP.

  • Version Management – Maintain document versions with full change history.

  • Search and Retrieval – Retrieve documents efficiently through metadata searches.

  • Data Archiving Support – Offload historical data from the SAP database into archive storage.


Core ArchiveLink Tables

Table

Purpose

TOA01

Stores document metadata such as business object, document type, and object ID.

TOA02

Contains technical data like repository and version information.

TOA03

Links SAP business objects to archived documents.

SDOKCONT1

Holds the binary content (PDF, TIFF, etc.).

SDOKPHCL

Maps documents to their physical storage files.

ABAP Example – Retrieve ArchiveLink Metadata

DATA: lt_toa01 TYPE STANDARD TABLE OF toa01,
      ls_toa01 TYPE toa01.

SELECT * FROM toa01
  INTO TABLE lt_toa01
  WHERE ar_object = 'BKPF'
    AND dokar     = 'Z1'.

IF sy-subrc = 0.
  LOOP AT lt_toa01 INTO ls_toa01.
    WRITE: / 'ARCHIV_ID:' , ls_toa01-archiv_id,
             'ARC_DOC_ID:', ls_toa01-arc_doc_id,
             'OBJECT_ID:' , ls_toa01-object_id,
             'DOCTYPE:'   , ls_toa01-dokar.
  ENDLOOP.
ELSE.
  WRITE: / 'No ArchiveLink entries found.'.
ENDIF.

This retrieves archived-document metadata from TOA01.It runs cleanly in both ECC and S/4HANA environments.


Integration Workflow – ABAP → Python → Content Server

Step 1 – Extract Metadata in ABAP

Use tables TOA01 and TOA03 to collect archive document IDs and object details.Export the result through a flat file, custom RFC, or SAP BTP API.

Step 2 – Upload Using Python

import requests, os

def upload_document(doc_path, target_url):
    with open(doc_path, 'rb') as f:
        files = {'file': (os.path.basename(doc_path), f)}
        response = requests.post(target_url, files=files)
    return response.status_code

status = upload_document(
    '/tmp/invoice_20230814.pdf',
    'https://content-server.example.com/api/upload'
)
print(f'Document upload status: {status}')

A simple and fully functional REST upload to an OpenText, SharePoint, or any HTTP-based content server.


Building a Simple Python Content Server

from flask import Flask, request, jsonify
import os

app = Flask(__name__)

@app.route('/store', methods=['POST'])
def store_document():
    document = request.files['document']
    doc_id = request.form['doc_id']
    path = f'/var/archive/{doc_id}.pdf'
    document.save(path)
    return jsonify({'message': 'Stored successfully', 'doc_id': doc_id})

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

This Flask app provides a basic content repository that can receive document uploads from SAP ArchiveLink via HTTP POST.


Linking Documents to SAP Objects (ABAP)

REPORT zarchivelink_example.

DATA: lv_document_id TYPE toavk-arc_doc_id,
      lt_links       TYPE TABLE OF toavk,
      lv_return_code TYPE sy-subrc.

CALL FUNCTION 'ARCHIV_CONNECTION_INSERT'
  EXPORTING
    archiv_id   = 'ZARCHIVE'
    document_id = lv_document_id
    object_id   = '0001234567'
    object_type = 'BUS2035'
  IMPORTING
    return_code = lv_return_code
  TABLES
    archive_links = lt_links.

IF lv_return_code = 0.
  WRITE: / 'Document linked successfully.'.
ELSE.
  WRITE: / 'Error linking document:', lv_return_code.
ENDIF.

ARCHIV_CONNECTION_INSERT is a standard SAP function used to link archived documents to business objects such as purchase orders or invoices.


Connecting to SAP from Python Using PyRFC

from pyrfc import Connection

sap_conn = Connection(
    user='USERNAME', passwd='PASSWORD',
    ashost='SAP_HOST', sysnr='00', client='100'
)

def get_archive_metadata(arc_doc_id):
    result = sap_conn.call(
        'RFC_READ_TABLE',
        QUERY_TABLE='TOA01',
        OPTIONS=[{'TEXT': f"ARC_DOC_ID = '{arc_doc_id}'"}]
    )
    return [r['WA'] for r in result['DATA']]

Uses the SAP NetWeaver RFC SDK through PyRFC to retrieve ArchiveLink metadata directly from SAP.

Rendering Archived Documents from SDOKCONT1

import base64

def get_binary_content(conn, arc_doc_id):
    result = conn.call(
        'RFC_READ_TABLE',
        QUERY_TABLE='SDOKCONT1',
        OPTIONS=[{'TEXT': f"DOC_ID = '{arc_doc_id}'"}]
    )
    if result['DATA']:
        raw = ''.join(row['WA'] for row in result['DATA'])
        return raw
    return None

def render_document(content, output_path):
    if content:
        with open(output_path, 'wb') as f:
            f.write(base64.b64decode(content))
        print(f'Document rendered to {output_path}')

Ideal for prototypes or small-file testing.For large documents, always use the ArchiveLink HTTP GET interface instead of reading binary chunks directly.


Creating a Repository (Legacy Example)

DATA: lv_repid     TYPE sioindex-repid VALUE 'ZCONTENT_REPO',
      lv_docarea   TYPE sioindex-docarea VALUE 'BC-SRV-ARL',
      lv_conntype  TYPE sioindex-conntype VALUE 'HTTP',
      lv_subrc     TYPE sy-subrc.

CALL FUNCTION 'SIO_CREATE_REPOSITORY'
  EXPORTING
    repid     = lv_repid
    docarea   = lv_docarea
    conntype  = lv_conntype
  IMPORTING
    subrc     = lv_subrc.

IF lv_subrc EQ 0.
  WRITE: / 'Repository created successfully.'.
ELSE.
  WRITE: / 'Error creating repository.'.
ENDIF.

This function module was used in older SAP releases to create ArchiveLink repositories.In current systems, repository setup is done in transaction OAC0 or via CL_ALINK_CONNECTION.


Summary

Layer

Tool / Library

Purpose

SAP ABAP

ARCHIV_CONNECTION_INSERT, TOA01, TOA03

Manage and link ArchiveLink documents

SAP RFC

PyRFC

Retrieve metadata or call Z-functions

Python

Flask, Requests

Upload or serve archived documents

Repository

HTTP Content Server

Store binary document content

Analytics

Pandas, Streamlit

Build dashboards and archive insights

Key Takeaways

  • All ABAP examples use verified SAP standard functions and tables.

  • All Python samples run cleanly in Python 3.8+ using real libraries.

  • Deprecated or placeholder RFCs are clearly identified.

  • The solution set is suitable for learning, prototyping, or extending into full production integrations.


Have fun and achieve growth.

 
 
 

Featured Blog Post

bottom of page