Wednesday, February 07, 2007

MS CRM 3.0: Callout's Configuration

As you know that you won't be able to use configuration settings from web.config, since Callout is executed inside CRM 3.0. I am not sure how the execution of Callout, but I suspect it's still under w3wp process, but it's already handled by CRM 3.0 execution and we are not able to use common function in ASP.NET. It's a common requirement that we need to put some configuration like connection string in some xml file, and it's common practice that this configuration is not hardcoded. So, how do you store and access this configuration. Here I share what I have done for my existing solution:

Create one xml file, for example custom.config, and copy this file to the same directory as your callout library:

<connectionstring>your connection string</connectionstring>

You can access the value of Connection String by writing this code in your callout:
XmlDocument doc = new XmlDocument();
string path = System.Reflection.Assembly.GetCallingAssembly().Location + + @"/custom.config";
// set XPath to point to ConnectionString Node
XmlNode node = doc.SelectSingleNode("//Configuration/ ConnectionString");
string connString = node.InnerText;

If you have more than one configuration, you can add new node inside <configuration> and set the XPath in the code accordingly. Happy configuring!