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

Hints, Tips and Tricks

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

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

  • Comments 9
  • Likes

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");
}
}

Comments
  • Hi

    This can be done a lot easier by adding in a reference to eWare.dll in your project. Then the above code becomes:

    using System;

    using eWare;

           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 {

                   eWareBase eware=(eWareBase)Activator.CreateInstance(Type.GetTypeFromProgID(crmProgID));

                   eware.FastLogon=3;

                   string logonresult=eware.Logon(adminUserName,adminPassword);

                   eWareQuery query=eware.CreateQueryObj(updatePasswordString,eware.Database);

                   query.ExecSql();

               }

               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");

               }

           }

  • Any idea why i get a null objCRMClassType? I am using CRM 7.2

    Type objCRMClassType = Type.GetTypeFromProgID(crmProgID);

    object objCRM = Activator.CreateInstance(objCRMClassType);

  • I haven't looked at this for some time so things may have changed.  I originally wrote this with Sage CRM 6.1 I think.

  • Can you teach me if there's any way that I could troubleshoot the prog ID?

    string crmProgID = "eWare.CRM61";

  • Oh!  The crmProgID is the registered instance of Sage CRM.  If you have installed CRM and called it 'CRM' then this will be

    eWare.CRM

    if you installed CRM and called it CRMX3 then it will be called

    eWare.CRMX3

  • I installed CRM 7.2 so should the ID be eware.CRM72? Even so, it didnt seemed to work.

  • What did you call the instance of Sage CRM?  If when you installed it you kept the default name, it would be eWare.CRM, but if you called it CRM72 then it would be eWare.CRM72

    But as I said I've not looked at this since 2009.

  • Jeff,

    I realize it has been a long time since you wrote this post. But I hope you can help.

    Trying to call eWare.CRM using the code above fails.  I receive a class not registered error when calling object objCRM = Activator.CreateInstance(objCRMClassType);

    I have Sage 6.1

    Using Visual Studio 2015.

    Jeff

  • I am sorry but I don't have any suggestions.