[Vtigercrm-developers] Upload document via API?

Tom Brown support at vtexperts.com
Tue Aug 2 18:52:25 GMT 2022


Chris,

See attached createDocApi.php.txt and the POST example.

Hopefully this helps.

Thanks


On Tue, Aug 2, 2022 at 8:21 AM socialboostdk <socialboostdk at gmail.com>
wrote:

> Hi there,
>
> Anyone have successfully uploaded a document via the API? (version 7.x)
>
> I've not been able to find any examples of it :-(
>
>
> Thank you!
>
> Best,
> Chris
> _______________________________________________
> http://www.vtiger.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20220802/f69ad5be/attachment-0001.html>
-------------- next part --------------
<?php
include_once 'includes/Loader.php';
include_once 'include/Zend/Json.php';
include_once 'vtlib/Vtiger/Module.php';
include_once 'include/utils/VtlibUtils.php';
include_once 'include/Webservices/Create.php';
include_once 'modules/Webforms/model/WebformsModel.php';
include_once 'modules/Webforms/model/WebformsFieldModel.php';
include_once 'include/QueryGenerator/QueryGenerator.php';
include_once 'includes/runtime/EntryPoint.php';
include_once 'includes/main/WebUI.php';
include_once 'include/Webservices/AddRelated.php';
$user = Users::getActiveAdminUser();
$current_user = Users_Record_Model::getCurrentUserModel();
if((int)$current_user->id==0){
    $current_user = $user;
}
try {
    $uploadedFiles = $_FILES;
    $module = 'Documents';
    $returnURL = '';
    foreach ($uploadedFiles as $fileFieldName => $uploadedFile) {
        if ($uploadedFile['error'] == 0 && $uploadedFile['name']) {
            $data['notes_title'] = $uploadedFile['name'];
            $data['document_source'] = 'Vtiger';
            $data['filename'] = $uploadedFile['name'];
            $data['filelocationtype'] = 'I';
            $data['source'] = 'API';
            $data['assigned_user_id'] = 1;
            $data['filestatus'] = 1;
            $recordId = vtws_create($module, $data, $current_user);
        }
    }
    sendResponse($returnURL, 'ok');
}catch (DuplicateException $e) {
    sendResponse($returnURL, false, $e->getMessage());
    return;
} catch (Exception $e) {
    sendResponse($returnURL, false, $e->getMessage());
    return;
}
function sendResponse($url, $success = false, $failure = false) {
    if ($success)
        $response = Zend_Json::encode(array('success' => true, 'result' => $success));
    else
        $response = Zend_Json::encode(array('success' => false, 'error' => array('message' => $failure)));

    // Support JSONP
    if (!empty($_REQUEST['callback'])) {
        $callback = vtlib_purify($_REQUEST['callback']);
        echo sprintf("%s(%s)", $callback, $response);
    } else {
        echo $response;
    }
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: POST_Example_createDocAPI.png
Type: image/png
Size: 120251 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20220802/f69ad5be/attachment-0001.png>


More information about the vtigercrm-developers mailing list