How to Log into Dynamics CRM from a ColdFusion Form

First off, this isn’t anything magical but the most difficult part will be getting ColdFusion to recognize the .NET .dll.  I’ll offer some solutions and workarounds that I came up with while getting this to work.

  1. In Visual Studio 2013, create a Class Library in the Visual C# Templates I named my project, CRMColdFusionLogin.
    2015-12-03_09h55_57
  2. In Solution Explorer, I renamed the Class1.cs file to Login.cs. Agree to update the class name to Login as well.
    2015-12-03_09h58_47
  3. Right-click on the References folder and add the following assemblies:
Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Client
Microsoft.Xrm.Sdk
System.Runtime.Serialization
  1. In Login.cs, add the following using statements:
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System.Diagnostics;

 

  1. In the class Login, add the following method, GetSystemUserId()
static public Guid GetSystemUserId(string connectionString)
{
    // Connect to the CRM
    CrmConnection conn = CrmConnection.Parse(connectionString);
    Guid userId = new Guid();

    using (OrganizationService service = new OrganizationService(conn))
    {
        try
        {
            // Retrieve the systemuserid of the account
            userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
        }
        catch (Exception err)
        {
            Debug.WriteLine(err.Message);
        }
    }
    return userId;
}

 

  1. I use Costura.Fody to combine all of the .NET assemblies and add it through the Package Manager Console. Open the Package Manager Console and enter, Install-Package Costura.Fody at the command prompt.  Then enter the command, Install-CleanReferencesTarget and choose to Reload the project.
    2015-12-03_10h47_54
  2. Build the Solution and open the …\Project\CRMColdFusionLogin\bin\Debug folder and confirm only one .dll was created.
    2015-12-03_10h50_58
  3. The easy part is done. Copy the CRMColdFusionLogin.dll file to your web server running ColdFusion.  When no path to the assembly is specified, ColdFusion will look for the .dll in the folder, <ColdFusion Folder>\cfusion\wwwroot\WEB-INF\cfclasses\dotNetProxy.  For now you can place it there and adjust it later.
  4. Create a ColdFusion file named, CrmLogin.cfm on the web server. Add the following code:
<cfif IsDefined("form")>
    <cfif StructKeyExists(form,"username") and Len(Trim(form.username)) and
        StructKeyExists(form,"password") and Len(Trim(form.password))>
        <cftry>
            <cfobject
                action="create"
                type=".NET"
                name="CrmLogin"
                class="CRMColdFusionLogin.Login"
                assembly="CRMColdFusionLogin.dll" />
 
                <cfset connectionString = "Url=http://crm/TEST; Domain=TEST; Username=" & form.username & "; Password=" & form.password & ";" />
 
            <cfoutput>#CrmLogin.GetSystemUserId(connectionString)#</cfoutput>
 
            <cfdump var="#CrmLogin#" />
 
            <cfcatch>
                <cfdump var="#cfcatch#" />
            </cfcatch>
        </cftry>
    </cfif>
</cfif>
 
<form action="CrmLogin.cfm" method="post">
    Username<br />
    <input type="text" name="username" size="30" maxlength="60" /><br />
    Password<br />
    <input type="password" name="password" size="30" maxlength="60" /><br />
    <br />
    <button type="submit" name="Login">Login</button>
</form>

 
Notice in the <cfobject> tag the name attribute can be anything you want, I chose “CrmLogin“.  The class attribute must contain the namespace and class “CrmColdFusionLogin.Login“.  The assembly attribute can contain a file path to the .dll if you do not want to place it in the default folder, <ColdFusion Folder>\cfusion\wwwroot\WEB-INF\cfclasses\dotNetProxy.  If you want to use a file path, change the backslashes to forward-slashes like this:

assembly="C:/wwwroot/CrmLogin/CrmColdFusionLogin.dll"

 

  1. A successful login should output the SystemUserId Guid and a ColdFusion dump of the object will look something like this:
    2015-12-03_11h02_00

ColdFusion Errors for .NET Integration

 
If you get an error message like, “DotNet Side does not seem to be running” or “Class CrmColdFusionLogin not found in the specified assembly list” check the following:

  1. Access the web server and confirm that the ColdFusion 10 .NET Service is running
    2015-12-03_11h32_35
  2. Restart the ColdFusion 10 .NET Service and see if that corrects the problem.
  3. Confirm that the ports specified in the file, JNBDotNetSide.exe.config located in the folder, <ColdFusion Folder>\cfusion\jnbridge and in the file, neo-dotnet.xml located in the folder, <ColdFusion Folder>\cfusion\lib match.
    2015-12-03_11h39_37
  4. If changes to either file are needed, stop all ColdFusion services and delete all .jar files expect for dotNetCoreProxy.jar in the folder, <ColdFusion Folder>\cfusion\wwwroot\WEB-INF\cfclasses\dotNetProxy
    2015-12-03_11h57_16

Start all ColdFusion services or restart the web server.

Leave a Reply

Your email address will not be published. Required fields are marked *