Best way to get all documents uploaded in Sugarcrm instance and import into onedrive

Hi All,

Can anyone suggest best way to get all documents uploaded in Sugarcrm instance and import into onedrive.

Background is that we are moving from sugarcrm to Microsoft CRM dynamics 365, so need a way to get all documents stored in sugarcrm and transfer into dynamics 365.

And we are thinking to store all documents in One drive.

Kindest Regards,

Shreya

  • For the existing documents, you can download them with the SugarCRM REST API one by one and then upload each document with the One-Drive-API. 

    This can be done offline in any development environment (php, c#, java, ...). It takes some time to run, depending on the number and size of the documents you have.

    For the new incoming documents you can write a logic hook in the documents or notes or emails module which transfers all new documents to One-Drive by the One-Drive-API.

  • Hi Harald,

    Thanks for responding!!

    Please see my queries below:

    1. Can you please let me know how to download all documents stored in sugarcrm using API?
    2. After downloading how we can map downloaded file to document record?
    3. As I can see documents are stored in /upload directory but that files are without extension, but I want downloaded file with extension, how to achieve it?

    Note: Apology for typo mistake but we want to store documents in sharepoint and then move it to Microsoft CRM dynamics, do you have any idea from migrating documents?

    Kindest Regards,

    Shreya

  • The API endpoint to download the list of documents (files are saved in DocumentRevsions) is

    GET .../rest/v10/DocumentRevisions

    Each single file can be downloaded by

    GET .../rest/v10/DocumentRevisions/:revison_id/file/filename

    (Yes the API is strange, I agree).

    Then the CURL request which calls the API returns the binary content of the file which must be saved after the CURL call.

    To imagine how it works I created a tine php file which runs e.g. in the webserver php environment. It loads all DocumentRevsions files to a subdir savearea, which you should create before you call the php script.

    So, just copy it to ...htdocs/<anypath>/get_documents.php,

    create ...htdocs/<anypath>/savearea,

    make sure this directoy is writable by the webserver,

    and call http://<yourhost>/<anypath>/get_documents.php

    It rerieves ALL documents, so for production puposes you should add some paging and/or filtering logic.

    Here is the script get_documents.php:

    <?php
    $base_url = "http://localhost/sugardev1300/rest/v10";
    $username = "jim";
    $password = "jim";
    
    ini_set('max_execution_time', 0);
    
    /////////////////////////////
    //Login - POST /oauth2/token
    /////////////////////////////
    $url = $base_url . "/oauth2/token";
    $oauth2_token_arguments = array(
        "grant_type" => "password",
        "client_id" => "sugar",
        "client_secret" => "",
        "username" => $username,
        "password" => $password,
        "platform" => "integration"  //must be defined in admin - configure API platforms
    );
    $oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);
    
    ///////////////////////////////
    //Read list of documents
    ///////////////////////////////
    $offset = 0;
    while ($offset >= 0){
    		$url = $base_url . "/DocumentRevisions";
    		$note_arguments = array(
    		"max_num" => 10,
    		"offset" => $offset,
    	);
    	$notes_response = call($url, $oauth2_token_response->access_token, 'GET', $note_arguments);
    	$offset = $notes_response->next_offset;
    	
    	echo "<hr>".print_r($notes_response,true)."<hr>";
    	
    	if (!empty($notes_response->records)){
    		foreach($notes_response->records as $idx => $note){
    			///////////////////////////////
    			//Read one document revison and save it as :docid__:revid__:filename in subdir ./savearea
    			///////////////////////////////
    			$doc_id = $note->document_id;
    			$rev_id = $note->id;
    			$rev_name = $note->filename;
    
            echo  "<br>\n" . $idx . " # " . $doc_id . " # " . $rev_id . " # " . $rev_name . "\n<br>";
    
    			$url_file = $base_url . "/DocumentRevisions/" . $rev_id . "/file/filename";
    			$file_response = call($url_file, $oauth2_token_response->access_token, 'GET', array(), false, true);
    			// $file_response contains the binary file because of return_binary=true
     	
    //			echo "##>".bin2hex($file_response)."<##\n";
    	
    			$file = fopen("./savearea/".$doc_id."__".$rev_id."__".$rev_name,"w");
    			fwrite($file, $file_response);
    			fclose($file);
    
    		}
    	}
    }
    
    ////////////////////////////////////////////////////////////////////
    // END OF MAIN
    ////////////////////////////////////////////////////////////////////
    
    
    /**
     * Generic function to make cURL request.
     * @param $url - The URL route to use.
     * @param string $oauthtoken - The oauth token.
     * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.
     * @param array $arguments - Endpoint arguments.
     * @param array $encodeData - Whether or not to JSON encode the data.
     * @param array $returnHeaders - Whether or not to return the headers.
     * @return mixed
     */
    function call(
        $url,
        $oauthtoken='',
        $type='GET',
        $arguments=array(),
        $encodeData=true,
        $returnHeaders=false,
    	$returnbinary=false
    )
    {
        $type = strtoupper($type);
    
        if ($type == 'GET')
        {
            $url .= "?" . http_build_query($arguments);
        }
    
        $curl_request = curl_init($url);
    
        if ($type == 'POST')
        {
            curl_setopt($curl_request, CURLOPT_POST, 1);
        }
        elseif ($type == 'PUT')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
        }
        elseif ($type == 'DELETE')
        {
            curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
        }
    
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);  // wichtig
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($curl_request, CURLOPT_ENCODING, '');
    
        if (!empty($oauthtoken)) 
        {
            curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("oauth-token: {$oauthtoken}","Content-Type: application/json"));
        }
        else
        {
            curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
        }
    
        if (!empty($arguments) && $type !== 'GET')
        {
            if ($encodeData)
            {
                //encode the arguments as JSON
                $arguments = json_encode($arguments);
            }
            curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
        }
    
        $result = curl_exec($curl_request);
    		
    /* only for debugging the curl call
    echo "<hr>";
    echo "###CALL###";
    echo $url;
    echo "<hr>";
    print_r($curl_request);
    echo "<hr>";
    print_r($arguments);
    echo "<hr>";
    print_r($result);
    echo "<hr>";
    */
    
        if ($returnHeaders)
        {
            //set headers from response
            list($headers, $content) = explode("\r\n\r\n", $result ,2);
            foreach (explode("\r\n",$headers) as $header)
            {
                header($header);
            }
    
            //return the nonheader data
            return trim($content);
        }
    
        curl_close($curl_request);
    
        if ($returnbinary)
    		$response = $result;  
    	else
        {
    		//decode the response from JSON
    		$response = json_decode($result);
    	}
    
        return $response;
    }
    ?>

  • Try the Skyvia app. It can OneDrive and Sugar CRM without coding. Read more here.