Node.js SugarCRM: SugarCRM Get MetaData

Back to Index

Retrieves SugarCRM metadata and saves (streams) the JSON metadata to a file. The metadata is quite large. For our sample SugarCRM database, the JSON metadata is approx. 3.5MB. Among many other things, the metadata contains the field names for each SugarCRM module.

For your convenience, Chilkat put the output of this sample code here: Sample SugarCRM Metadata JSON. Load this URL in FireFox to browse the JSON. (FireFox provides nice JSON browsing capability..)

Note: This example uses our own demo SugarCRM domain containing demo data. You should replace "cscspanset.demo.sugarcrm.eu" with your own domain.


var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    success = rest.Connect("cscspanset.demo.sugarcrm.eu",443,true,true);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    rest.AddQueryParam("type_filter","modules");

    rest.AddHeader("Cache-Control","no-cache");
    rest.AddHeader("OAuth-Token","OAUTH2_ACCESS_TOKEN");

    //  First send the HTTP request.
    success = rest.SendReqNoBody("GET","/rest/v10/metadata/");
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    //  Read the response header.  If the response status code is success, stream to the file.
    //  Otherwise receive the error response text.
    var statusCode = rest.ReadResponseHeader();
    if (statusCode < 0) {
        console.log(rest.LastErrorText);
        return;
    }

    if (statusCode !== 200) {
        console.log("Received error response code: " + statusCode);
        //  Read the error response body.
        var sbErrResponse = new chilkat.StringBuilder();
        success = rest.ReadRespSb(sbErrResponse);
        if (success !== true) {
            console.log(rest.LastErrorText);
            return;
        }

        console.log("Error response:" + sbErrResponse.GetAsString());
        return;
    }

    //  Stream the response body to the output file.
    var respBodyStream = new chilkat.Stream();
    respBodyStream.SinkFile = "sugarcrm_metadata.json";
    success = rest.ReadRespBodyStream(respBodyStream,true);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    console.log("Example Completed.");

}

chilkatExample();