Python CMIS Scripts for Alfresco

Introduction

CMIS stands for Content Management Interoperability Services.  Using CMIS within Python scripts is a handy way to export/import data from/to Alfresco (or any other CMIS-compliant document management system) or do mass updates of data within Alfresco.  This can be changes to document attributes or even changes to the documents themselves.  If you come from a relational database background, think of CMIS scripts fulfilling a similar role with Alfresco as SQL scripts do with relational databases.

Python cmislib from Apache Chemistry is a CMIS client library for Python.  This tutorial shows you how to install Python and CMIS on a Ubuntu computer.

Why Python?

Python is an elegant readable language, with an emphasis on simplicity, and I think for that reason has become very popular. It is my personal preference for writing CMIS scripts.

From the author: Guido Van Rossum
http://python-history.blogspot.com.au/2009/01/pythons-design-philosophy.html

The Zen of Python:
https://www.python.org/dev/peps/pep-0020/

(You can also use CMIS within java, javascript and PHP.)

Python and CMIS Installation

  • install setuptools for python
sudo apt-get install python python-setuptools
  • install cmislib
 
sudo easy_install cmislib
  • download the most recent version of cmislib-alfresco-extension
wget https://github.com/pcollardez/cmislib-alfresco-extension/archive/master.zip
  • install cmislib-alfresco-extension
sudo easy_install master.zip

Test Python and CMIS

Test your installation by running the script below that lists the sites in your Alfresco system.

  • paste the code into a file called test.py
  • change the URL to your Alfresco server
  • change the ‘password’ to the correct password for your Alfresco system
#!/usr/bin/env python
# Alfresco extension to CMIS
import cmislibalf

from cmislib import CmisClient, Repository, Folder
from cmislib.model import CmisId
from cmislib.exceptions import CmisException, UpdateConflictException
import os, sys

try:
  client = CmisClient('http://domain.com.au:8080/alfresco/cmisatom', 'admin', 'password')
  repo = client.defaultRepository
except:
  print "failed to connect to Alfresco"
  quit()

try:
  print 'Getting list of sites ..'
  folders = repo.query("select * from cmis:folder where cmis:objectTypeId='F:st:site'")
  print 'Processing list ..'
  for folder in folders:
    obj = repo.getObject(folder.id)
    # properties items are key-value TUPLES ..
    for key,val in obj.properties.items():
      if key=='cm:name' or key=='cm:title' or key=='cmis:path':
        print key,'=',val
except ValueError:
  print "failed to read site list"
  • make test.py executable
chmod +x test.py
  • run the script
./test.py
  • output from the script will look something like ..
Getting list ..
Processing list ..
cmis:path = /Sites/swsdp
cm:title = Sample: Web Site Design Project
cmis:path = /Sites/gpc
cm:title = GPC
cmis:path = /Sites/digs
cm:title = DIGS

Other Resources

Apache Chemistry has some good tutorials and example code ..
http://chemistry.apache.org/python/docs/examples.html#get-a-repository-object

CMIS Workbench is a great tool for querying Alfresco and doing quick edits.  It is also a very useful tool for determining attributes of objects while writing CMIS scripts.  You can download from http://www.apache.org/dyn/closer.cgi/chemistry/opencmis/0.13.0/chemistry-opencmis-workbench-0.13.0-full.zip

CMIS Workbench

Screenshot of CMIS Workbench

Order of the Bee – an independent organisation of the Alfresco community. Web-site includes good technical and non-technical posts and other information, focussed mainly on Alfresco Community Edition (CE).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s