Inside Microsoft Dynamics CRM 3.0

Arne Janning

  Home  |   Contact  |   Syndication    |   Login
  3 Posts | 0 Stories | 13 Comments | 36 Trackbacks

News

Archives

MSCRM blogs I read

So... I didn't post for a long time, I have been on holidays since new year, didn't have an internet connection for a long time - I actually write this from an internet cafe.

In my last post I promised to show how to get inside CRM and do customizations that go far beyond of what is supported. I also already mentioned the main entry point of the whole CRM-web-application, which is the Microsoft.Crm.MainApplication.Application_OnStart()-method in Microsoft.Crm.Application.Pages.dll. Microsoft.Crm.MainApplication is referenced in the global.asax-file in the CRM-webroot:

<%@ Application language="c#" Inherits="Microsoft.Crm.MainApplication" CodeBehind="Microsoft.Crm.Application.Pages.dll"%>

<%@ Application language="c#" Inherits="Microsoft.Crm.MainApplication" CodeBehind="Microsoft.Crm.Application.Pages.dll"%>

<%@ Application language="c#" Inherits="Microsoft.Crm.MainApplication" CodeBehind="Microsoft.Crm.Application.Pages.dll"%>

<%@ Application language="c#" Inherits="Microsoft.Crm.MainApplication.Application" CodeBehind="Microsoft.Crm.Application.Pages.dll"%>

We will write our own CRM-host to get inside CRM and get access to the internal CRM-object model at runtime. This is fairly easy.

First of all, create class library project in Visual Studio .NET 2003 - it must be .NET 1.1. In my example the project has the name "Janning.Crm.Host".

Then make sure you have added the following references:

  • System.dll
  • System.Data.dll
  • System.XML.dll
  • System.Drawing.dll
  • System.Web.dll
  • Microsoft.Crm.dll (from the GAC)
  • Microsoft.Crm.Application.Components.Application.dll (from \bin)
  • Microsoft.Crm.Application.Components.Core.dll (from \bin)
  • Microsoft.Crm.Application.Components.Platform.dll (from \bin)
  • Microsoft.Crm.Platform.Sdk.dll (from the GAC)

There is a small trick to copy assemblies from the GAC : Press Start --> Run and then enter C:\Windows\assembly\gac. The shell extension which is normally running on the assembly-folder won't show up then and you can easily copy and paste the CRM-assemblies and reference them from VS.NET.

Then you have to add the following code into a codefile:

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using System.Web.SessionState;
using System.Xml;

using Microsoft.Crm;
using Microsoft.Crm.Application.Platform;
using Microsoft.Crm.Errors;
using Microsoft.Crm.Metadata;
using Microsoft.Crm.Security;
using Microsoft.Crm.Utility;

namespace Janning.Crm.Host
{
 public class CustomCrmApplicationHost : HttpApplication
 {
  protected void Application_Start(Object sender, EventArgs e)
  {
   RegControl.LoadLibraries();
   NotificationManager.ExtraParameter = HttpContext.Current;
   MetadataCacheConfig.LoadMethod = LoadMethod.Database;
   NotificationManager.StartNotificationsThread(new Notification());
  }

  protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
   try
   {    
    UserCache.GetCurrentUser();
   }
   catch (Exception ex)
   {
    COMException comex = ex as COMException;
    string errorCode = "0xffffffff";
    if (comex != null)
    {
     errorCode = comex.ErrorCode.ToString("x", CultureInfo.InvariantCulture);
    }
    string redirectPath = string.Format(
     CultureInfo.InvariantCulture,
     "/_common/error/authenticationError.htm?0x{0}&{1}",
     errorCode,
     base.Request.Url.AbsoluteUri);
    base.Response.Redirect(redirectPath);
    base.Response.End();
    CrmTrace.TraceFormat(
     TraceCategory.Application,
     TraceLevel.Error,
     "An error occurred during the Application_OnAuthenticateRequest : \nError: {0} \nStack Trace:{1}",
     ex.Message,
     ex.StackTrace);
   }
  }

  protected void Application_Error(Object sender, EventArgs e)
  {
   base.Response.Clear();
   Exception ex = base.Server.GetLastError();
   ErrorInformation info = new ErrorInformation(ex, base.Request.Url);
   if (ConfigurationSettings.AppSettings["DevErrors"] != "On")
   {
    info.StackTrace = string.Empty;
   }
   if (info.Source == "XML")
   {
    base.Response.ContentType = "text/xml";
    ErrorInformation.XmlSerializer.Serialize(base.Response.OutputStream, info);
    base.Response.End();
   }
   else if (info.Source == "SOAP")
   {
    base.Response.Clear();
    base.Response.ContentType = "text/xml";
    base.Response.StatusCode = 500;
    XmlTextWriter writer = new XmlTextWriter(base.Response.Output);
    writer.WriteStartDocument();
    writer.WriteStartElement("soap", "Envelope", "
http://schemas.xmlsoap.org/soap/envelope/");
    writer.WriteStartElement("Body", "
http://schemas.xmlsoap.org/soap/envelope/");
    writer.WriteStartElement("Fault", "
http://schemas.xmlsoap.org/soap/envelope/");
    writer.WriteElementString("faultcode", "Server");
    writer.WriteElementString("faultstring", info.Description);
    writer.WriteStartElement("detail");
    ErrorInformation.XmlSerializer.Serialize(writer, info);
    writer.WriteEndDocument();
    base.Response.End();
   }
   else
   {
    HttpException httpEx = ex as HttpException;
    string maxRequestLenghText = "Maximum request length exceeded.";
    string pathFileName = Path.GetFileName(base.Request.Path);
    if ((pathFileName == "print_data.aspx") || (base.Request.Path == "/crmreports/download.aspx"))
    {
     base.Response.ClearHeaders();
    }
    if ((pathFileName == "importFieldMap.aspx") && (httpEx.ErrorCode == -2147467259) && ((ex.InnerException != null) ? (maxRequestLenghText == ex.InnerException.Message) : true))
    {
     base.Server.ClearError();
     base.Response.Redirect("/Tools/BulkImport/importFileChoose.aspx?errorMessage=BulkImport_Error_Exceed_MaxFileSize");
     base.Response.End();
    }
    else if ((pathFileName == "upload.aspx") && (httpEx.ErrorCode == -2147467259))
    {
     base.Server.ClearError();
     base.Response.Redirect("/_common/error/uploadFailure.aspx?hr=0x80043e08");
     base.Response.End();
    }
    else if (pathFileName == "print_data.aspx")
    {
     base.Server.ClearError();
     base.Response.Redirect("/_common/error/popuperror.aspx?hr=" +
      httpEx.ErrorCode.ToString());
     base.Response.End();
    }
    else if (ConfigurationSettings.AppSettings["DevErrors"] == "On")
    {
     //left this out for the moment
    }
    else
    {
     base.Response.Redirect(
      "/_common/error/errorhandler.aspx?errNum=" +
      HttpUtility.UrlEncode(info.Code) +
      "&errMessage=" +
      HttpUtility.UrlEncode(info.Description));
     base.Response.End();
    }
   }
  }
 }
}

Compile the code into an assembly, copy the assembly into the \bin-folder, make a backup of the global.asax-file and change the global.asax-file to this:

<%@ Application language="c#" Inherits="Janning.Crm.Host.CustomCrmApplicationHost" CodeBehind="Janning.Crm.Host.dll"%>

<%@ Application language="c#" Inherits="Janning.Crm.Host.CustomCrmApplicationHost" CodeBehind="Janning.Crm.Host.dll"%>

<%@ Application language="c#" Inherits="Janning.Crm.Host.CustomCrmApplicationHost" CodeBehind="Janning.Crm.Host.dll"%>

<%@ Application language="c#" Inherits="Janning.Crm.Host.CustomCrmApplicationHost" CodeBehind="Janning.Crm.Host.dll"%>

(If your assembly-name or namespace is different you have to change this of course)

Make an iisreset (Start --> Run --> iisreset) and open CRM again: http://yourCrmServerOrIP

Everything just works exactly like it did before, there is only one difference: if you use a tool like Process Explorer and look which dlls are loaded into the w3wp.exe-process you'll see that the Janning.Crm.Host.dll is actually running in the process. As the code in this assembly which is running in the most central part of CRM is now completely under your control you can do pretty much anything - if you know the internal object model. I'll write about this soon in detail and give you a couple of examples.

I should mention again that all this is of course completely unsupported by Microsoft, me or my future employer.

If someone wants to have the complete VS.NET-solution-files then just leave a comment here and I'll send it via email. I have no access to an FTP-server at the moment so I can't offer a download - perhaps someone has some empty space for me? :-)

(And please, could somebody explain me how to use the font sizes in .Text-Admin - I simply don't get it)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Saturday, January 28, 2006 3:35 PM

Feedback

# re: Getting inside Microsoft CRM - Part I 1/28/2006 9:04 PM Shai
Please provide the complete VS.NET-solution-files .
TIA
Shai

# re: Getting inside Microsoft CRM - Part I 1/30/2006 6:33 AM Offline mode?
Have you tryied this for the cassiniclient? There is not support for offline CRUD thru sdk objects.
JDE

# re: Getting inside Microsoft CRM - Part I 2/2/2006 10:54 AM LIZ
For the Outlook client you cn use the metadataservice for access of the metabase, but the CrmService won't work. For that you need to use direct data access into the MSDE database...

# re: Getting inside Microsoft CRM - Part I 2/6/2006 4:58 AM Meteor
I need it.thx.

# re: Getting inside Microsoft CRM - Part I 2/7/2006 7:23 AM Alper Can
Please provide the complete VS.NET-solution-files .

alperc@datasistem.com.tr

# re: Getting inside Microsoft CRM - Part I 2/14/2006 3:22 PM Geert
I'm very interested in your code files.
my email: maeseleg at delaware.be

# re: Getting inside Microsoft CRM - Part I 2/15/2006 5:06 AM Jebin
Please provide the complete VS.NET-solution-files . That is very useful for me
my mail id.. jebin_blessed@yahoo.com


# re: Getting inside Microsoft CRM - Part I 3/8/2006 8:44 AM Piyush
Please provide the complete VS.NET-solution-files
My ID: piyushjazz_2002@rediffmail.com

Regards,
Piyush

# re: Getting inside Microsoft CRM - Part I 3/9/2006 9:54 PM Wil
I would like your VS.NET-solution-files. My email is vastalderaan@yahoo.com.

Thanks,

Wil

# re: Getting inside Microsoft CRM - Part I 3/13/2006 8:44 AM VenkataRaviKumar
Please provide the complete VS.NET-solution-files
My ID:venkata@punditsguild.com


# re: Getting inside Microsoft CRM - Part I 3/13/2006 8:47 AM VenkataRaviKumar
Please provide the complete VS.NET-solution-files
My ID: venkata@punditsguild.com
Thanks
venkataravikumar

# re: Getting inside Microsoft CRM - Part I 3/20/2006 9:46 AM Erik
Please provide the complete VS.NET-solution-files .
Thanks,
schinkel_erik@hotmail.com

# re: Getting inside Microsoft CRM - Part I 3/21/2006 11:46 AM fvharen
Please provide the complete VS.NET-solution-files .
Thanks,

Frank
fvharen@senet.nl

# re: Getting inside Microsoft CRM - Part I 3/22/2006 6:52 AM fvharen
I followed the procedure but I can't see where the Janning.Crm.Host.dll in the w3wp.exe proces is. I opened CRM, which works just fine.


# re: Getting inside Microsoft CRM - Part I 3/22/2006 11:13 AM Stéphane
Hi Arne,

This is a very nice way of getting access to the CRM host.

Could you send me the VS.Net files ?

Thanks in advance.
Stephane@dorrekens.com

# re: Getting inside Microsoft CRM - Part I 5/3/2006 2:06 PM wagner
Hi,

Very good job!
Could you send me the VS.Net files ?


xwagner@gmail.com

# re: Getting inside Microsoft CRM - Part I 5/26/2006 9:23 AM Robert
Could you send me the VS.Net files ?
undead@iafrica.com

when is Part 2 coming

# re: Getting inside Microsoft CRM - Part I 6/16/2006 5:48 AM Patel Vipul
Hi
Please provide the complete VS.NET-solution-files .

my email id is : vrp822001@gmail.com

# re: Getting inside Microsoft CRM - Part I 7/28/2006 12:54 PM Oguz
Hi,

Very good job!
Could you send me the VS.Net files ?


# re: Getting inside Microsoft CRM - Part I 8/31/2006 8:01 PM Shaine Fisher
Very impressive, this will alow me to experiment to my hearts content, please send the VS.Net files.
shainefisher@hotmail.com

# re: Getting inside Microsoft CRM - Part I 9/13/2006 4:39 AM Manish Dixit
Hi,
could you send me the VS.Net files ?

# re: Getting inside Microsoft CRM - Part I 9/14/2006 5:12 PM P
I'd really appreciate if you could send me the VS.Net solution

Thanks!

# re: Getting inside Microsoft CRM - Part I 9/14/2006 5:13 PM PC
Hi,
Can you please send me the VS.Net solution at purvichheda@gmail.com

Thanks!

# re: Getting inside Microsoft CRM - Part I 9/21/2006 8:16 PM GK
Hello-

Could you please send me the VS.NET files?
gk@lexitroninc.com

# re: Getting inside Microsoft CRM - Part I 1/9/2007 1:37 PM belkacem
hello

please can you send me the VS.Net solution at belk.mans@gmail.com

Thank you



# re: Getting inside Microsoft CRM - Part I 1/17/2007 10:09 PM Chane
I would love the source code.
Please send it to sbishop AT statera.com.

# re: Getting inside Microsoft CRM - Part I 2/28/2007 9:45 AM ledrahc
hope i could get a copy of your code.
richardlaunio@yahoo.com.ph

# re: Getting inside Microsoft CRM - Part I 3/5/2007 11:22 AM alvin
Please send me one copy as well. Thanks very muchl

alvinsue at hotmail dot com


# re: Getting inside Microsoft CRM - Part I 3/21/2007 10:28 AM Murali
Hi,
I like the way you get into the MS CRM. Great Job.

Do mail me the VS Solution file. murali.tk@gmail.com.

# re: Getting inside Microsoft CRM - Part I 4/18/2007 3:40 PM Abraham Saldana
Can you please send me VS.net the code and/or sample code, Also I can help you with your need for web space.

Great job!
Thanks in advance
Abe Saldana.

# re: Getting inside Microsoft CRM - Part I 8/29/2007 2:49 AM Anshul
Hi
Please provide the complete VS.NET-solution-files .

my email id is : anshul2005@gmail.com

# re: Getting inside Microsoft CRM - Part I 10/10/2007 6:47 AM Christian Havel
Hi,

please provide the complete VS.NET solution files.
My eMail is:
christian.havel@c4b.de
Thanks
Christian

# re: Getting inside Microsoft CRM - Part I 11/20/2007 9:15 PM Geraldine
Hi,

Please provide the complete VS.NET solution files.
My Email is:
milkywayz@hotmail.com

# re: Getting inside Microsoft CRM - Part I 2/21/2008 6:27 PM Kirubakar
Can you please send me the source code at the following mail id Kibs75@yahoo.com

# re: Getting inside Microsoft CRM - Part I 5/27/2008 1:35 AM Dhaval Desai
Please provide the complete VS.NET solution files.
My Email is:
dydesai@gmail.com


# re: Getting inside Microsoft CRM - Part I 6/5/2008 11:38 PM CRM Developer
Please upload the code files, that shall be really helpful. thanks.

# re: Getting inside Microsoft CRM - Part I 7/2/2008 2:39 PM Geron
Please provide the complete VS.NET solution files.
My Email is:
gprofet@zonnet.com


# re: Getting inside Microsoft CRM - Part I 7/2/2008 2:40 PM Geron
Sorry, little typo on my previous post.
Email should be: gprofet@zonnet.nl

# re: Getting inside Microsoft CRM - Part I 2/18/2009 4:33 PM johnny
Great job!!!!

Can you send me the source at johnny3627@gmail.com

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: