pythonalfrescocmis

Update a existing file in alfresco using Python


How do I update a existing file in alfresco using CMis through Python

Below is the method i use to upload files

def alfresco_post_file(url, user, pwd, root_folder, post_folder, post_file, file_name, logger):
        client = CmisClient(url, user, pwd)
        repo = client.defaultRepository    
        try:
            folder = repo.getObjectByPath('/'+root_folder)
            if post_folder:
                folder = repo.getObjectByPath('/'+post_folder)
            contents = open(post_file, 'r')
            file = folder.createDocument(file_name, contentFile = contents)
        except Exception as e:
            sys.exc_clear()
            if type(e).__name__ == 'ObjectNotFoundException':
                folder = repo.getObjectByPath('/'+root_folder)
                createfolder = folder.createFolder(post_folder.replace(root_folder+'/',''))
                folder = repo.getObjectByPath('/'+post_folder)
                contents = open(post_file, 'r')
                file = folder.createDocument(file_name, contentFile = contents)
            #elif type(e).__name__ == 'UpdateConflictException':
                #folder = repo.getObjectByPath('/'+post_folder)
                #contents = open(post_file, 'r')
                #file = folder.updateDocument(file_name, contentFile = contents)
            else:
                print("(alfresco_post_file) Execution failed:", e)
                logger.info('(alfresco_post_file) Execution failed:' + str(e));

Solution

  • The source code is often a good source for examples. In this case, I grabbed a few lines from one of the tests in cmislibtest.py:

    # check out the document to get its Private Working Copy (PWC):
    pwc = newDoc.checkout()
    
    # update the PWC with a new file
    f = open(testFile2, 'rb')
    pwc.setContentStream(f)
    f.close()
    
    # checkin the PWC
    newDoc = pwc.checkin()