Calling a Sage CRM COM object from a .NET project.

1 minute read time.

I wrote an article just a little while ago that discussed how a System Administrator could easily reset a users password. See: Resetting Passwords

Within the article was example code that demonstrated how an External COM logon could use a QueryObject to pass an SQL update statement into Sage CRM. The actual example was a java-script file (e.g. changepsswd.js) that was saved on the windows desktop that when doubled clicked would logon to Sage CRM and change a users password.

Below is a development of this idea. This is a snippet of code that shows how you can invoke the Sage CRM COM object (as an external logon) from a .NET project.

It is assumed you are writing a .NET windows application or an ASP.NET application.

This code uses C# Reflection and late binding of the eware.Instance object.

Example C# Code

using System.Reflection;

private void button1_Click(object sender, EventArgs e)
{

string adminUserName = "Admin";
string adminPassword = "";
string crmProgID = "eWare.CRM61";
string updatePasswordString = @"update users set user_password = 'fish' where user_userid =4";

try
{
Type objCRMClassType = Type.GetTypeFromProgID(crmProgID);
object objCRM = Activator.CreateInstance(objCRMClassType);
object objQuery;

//Set the FastLogon Property to 3. This prevents meta data from loading.
object[] paramFastLogon = new Object[1];
paramFastLogon[0] = 3;
objCRM.GetType().InvokeMember("FastLogon", BindingFlags.SetProperty, null, objCRM, paramFastLogon);

//Call the Logon method
object[] paramLogon = new Object[2];
paramLogon[0] = adminUserName;
paramLogon[1] = adminPassword;

objCRM.GetType().InvokeMember("Logon", BindingFlags.InvokeMethod, null, objCRM, paramLogon);

//Get the Query object

object[] paramGetQueryObject = new Object[1];
paramGetQueryObject[0] = updatePasswordString;

objQuery = objCRM.GetType().InvokeMember("CreateQueryObj", BindingFlags.InvokeMethod, null, objCRM, paramGetQueryObject, null);

objQuery.GetType().InvokeMember("ExecSQL", BindingFlags.InvokeMethod, null, objQuery, null, null);

}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}