Moving files into the Library using the serverside COM API.

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Moving files into the Library using the serverside COM API.

  • Comments 12
  • Likes


A customer had a requirement to be able to automate the inclusion of existing files into the CRM library structure.  The scenario is that a file is created by another business process e.g. "Northwind Order Processing".  That file then has to be added to CRM.

This is not the first time I have written about the library and this article draws on some of the research that I have carried out previously:

The following is assumed:

  • The external system has an 'event' that can be used as the file is created.
  • The filename is known e.g. ABC123456.pdf
  • The file is created in a known folder e.g. c:\otherapp\
  • The external system and Sage CRM are integrated.  For example, the file is created for a record in the external system that has a corresponding record in Sage CRM.  The external file that needs to be imported might be created for the company "Alfred's Futterkiste" in the Northwind database that is linked to the company record in Sage CRM for "Alfred's Futterkiste" by the Sage CRM foreign field comp_northwindcompanyid = "ALFKI".


This is a simple proof of concept.  I have written this using javascript but the technique could be used by any language that can use COM.

The tasks we have to do are to 

  1. Find the library path
  2. Find the company specific library.
  3. Copy the file into library folders
  4. Add to the document library table and link to correct company/person/etc
  5. Add a communication

The code carries out an external logon which is the type of logon that a script run from a xxx.js file on the desktop might use.

Example Code

var username = "Admin";
var password = "";
var CRM = new ActiveXObject("eWare.CRM");
CRM.FastLogon = 3;  //this prevents the meta data from loading.
var recUser = CRM.FindRecord("user","user_logon='"+username+"'");
var strNorthwindCompanyID = "ALFKI";
var strfilename = "abc123456.txt";
var strpathname = "c:\\otherapp\\";
var strsourcefullpath = strpathname+strfilename;
//get document library path
var recSysParams = CRM.FindRecord("custom_sysparams","parm_name='docstore'")
var strCRMLibraryRoot = recSysParams.parm_value;
var recCompany = CRM.FindRecord("company","comp_northwindcustomerid='"+strNorthwindCompanyID+"'");
var strCompPath = recCompany.comp_librarydir;
var strdestinationfullpath = strCRMLibraryRoot +strCompPath +"\\"+strfilename;
var myObject = new ActiveXObject("Scripting.FileSystemObject");
//create folder if it doesn't exist
var arrCompPath = strCompPath.split("\\");
if(!myObject.FolderExists(strCRMLibraryRoot +arrCompPath[0]))
myObject.CreateFolder (strCRMLibraryRoot +arrCompPath[0])
if(!myObject.FolderExists(strCRMLibraryRoot +strCompPath))
myObject.CreateFolder (strCRMLibraryRoot+strCompPath)
//copy file
//syntax filesystemobject.CopyFile(source, destination, overwrite)
myObject.CopyFile (strsourcefullpath, strdestinationfullpath);
/////////create communication/////////////////
var myDate = new Date();
var recCommunication = CRM.CreateRecord("communication");
recCommunication.comm_action = "LetterOut";
recCommunication.comm_type = "Task";
recCommunication.comm_channelid = recUser.user_primarychannelid;
recCommunication.comm_status = "Complete";
recCommunication.comm_priority = "Normal";
recCommunication.comm_datetime = myDate.getVarDate();
recCommunication.comm_note = "details go here"
var recCommLink = CRM.CreateRecord("comm_link");
recCommLink.cmli_comm_userid = recUser.user_userid;
recCommLink.cmli_comm_communicationid = recCommunication.comm_communicationid;
recCommLink.cmli_comm_personid = recCompany.comp_primarypersonid;
recCommLink.cmli_comm_companyid = recCompany.comp_companyid;
//////////////Create Library Record
var recLibrary = CRM.CreateRecord("library");
recLibrary.libr_companyid= recCompany.comp_companyid;
recLibrary.libr_personid= recCompany.comp_primarypersonid;
recLibrary.libr_userid= recUser.user_userid;
recLibrary.libr_channelid= recUser.user_primarychannelid;
recLibrary.libr_type= "Letter";
recLibrary.libr_category= "Sales";
recLibrary.libr_filepath= recCompany.comp_librarydir;
recLibrary.libr_filename= strfilename;
recLibrary.libr_note= "upload";
recLibrary.libr_status = "draft";
recLibrary.libr_entity= "company";


  • Hi Jeff

    For Test record I have tried to add record to communication as below:

               addrecordRequest objAddRecordRequest = new addrecordRequest();

               addresult objAddResult = new addresult();

               addrecordResponse objAddRecordResponse = new addrecordResponse(objAddResult);

               ewarebase[] objRecords = new ewarebase[1];

               communication objCommunication = new communication();

               objCommunication.channelid = 1;


               objCommunication.status = comm_status.InProgress;

               objCommunication.priority = comm_priority.Normal;


               objCommunication.userid = 1;

               objCommunication.description = "Test Communication description"            

               objCommunication.note =  "Test Communication Notes";

               objRecords[0] = objCommunication;

               objAddRecordRequest.entityname = "Communication";

               objAddRecordRequest.SessionHeader = crmSessionHeader;

               objAddRecordRequest.records = objRecords;

               objAddRecordResponse = CRMService.addrecord(objAddRecordRequest);

    Though the code seems to insert the communication object but couldn't retrieve the inserted description, note. Any suggesstions please?

  • Raju

    A SOAP webservice question.  I can see from the standard wsdl definition that like description, note is defined as a String.  So they should both work in the same way.  Have you looked in the webservice log to see the XML that is passed and whether the 'Note' is included.  In the SQL log you will be able to see the insert statement that is executed and whether that contains the 'Note'.

  • Thanks Jeff. I have checked the sql logs for the insert statement and didn't seem to contain note,description fields in any statements.

    It seems to insert as below:




    (1,'20101207 00:47:32',1,'20101207 00:47:32','20101207 00:47:32',-2147483640,411)

    Then I tried to query one of the records with description and note fields with values as below:

                   objQueryEntityRequest.entityname = "communication";

          = pId;

                   objQueryEntityRequest.SessionHeader = crmSessionHeader;

                   objQueryEntityResponse = CRMService.queryentity(objQueryEntityRequest);

    But unfortunately the above didn't return the note and description values (only contains null values).

    In the other hand while querying for all the communication records as below:

    objQueryRecordRequest.fieldlist = "";

               objQueryRecordRequest.entityname =  "communication";

               objQueryRecordRequest.queryString =  "";

               objQueryRecordRequest.orderby = "";

               objQueryRecordRequest.SessionHeader = crmSessionHeader;

    objQueryRecordResponse = CRMService.queryrecord(objQueryRecordRequest);

    queryrecordresult myQueryRecordResult = objQueryRecordResponse.result;

               crmrecord[] myRecordList =  myQueryRecordResult.records;

    The above record list seems to return all the notes,description and rest of the fields.

    Why would this happen?

  • Raju

    This really should be a thread in the forums rather than a comment in an unrelated article.  I am not in a position to check at the moment.  But have you looked at the difference between using Add() and AddRecord()?

  • Hi Raju,

    Further to Jeff's response, i would suggest you post your query to the forum in the User Community called Questions about Sage CRM.



  • Thanks Jeff and Lucy. Will check out the forum in User Community.

  • Nice article.I was trying this and was getting an error "Runtime error: Automation server can't create object".Its shows error at line 4 i.e var CRM = new ActiveXObject("eWare.CRM");

    What could be the possible cause for this?

  • Nithin

    Is your install of Sage CRM called 'CRM'?  If your install is called CRMTest then the code would be

    var eWare = new ActiveXObject("eWare.CRMTest");

  • Hi Jeff,

    I am sucessfully using a version of this code on a few different servers. We recently moved a CRM 6.2L install to a Windows 2008 R2 and CRM seems to be fully functional.

    The only problem we are having is that the js file fails on the

    var CRM = new ActiveXObject("eWare.CRM");

    like just like whats happening to Nithin Punnoose.

    I'm guessing that I need to either configure the eWare com object to run from the script host or I have a permissions error. I've double checked Component services and the object is named eWare.CRM.


  • I just found the solution, I guess typing it out helped me think about the problem a bit more.

    On a 64 bit server if you call the default script engine it runs 64 bit which won't work. So you need to run the 32 bit script engine located in the syswow64 folder.

    For me I created a timer and execute: cscript importlibrary.js

  • This post is related to what you are talking about but in reference to the SOAP API.

    How do I access the Library table using the SOAP API?

  • See:

    If there are documents and files that need to be uploaded into the system the Web Services interface does not have any methods to handle file uploads and addition into the document library.  If you are working with on premise Sage CRM then you can use a 3rd party upload script to help you.  You will need to Google for an example script.