Implementing a Callout
As an example, here's a sample callout that appends sales leads to a text file whenever the lead status changes from "new" to a different status. Here, you'd use the
PostUpdate event of the lead entity to trigger the callout. To begin, you need to create a callout configuration file (if you don't have one already). To do that:
- Restart IIS
- Navigate to C:\ Program Files\Microsoft CRM\server\bin\assembly
Author's Note: All paths assume MSCRM is installed on your C:\ drive. |
- In that folder, create a new XML file named callout.config.xml.
- Open this file in notepad and paste the contents of the sample file callout.config.xml (available in the downloadable code that accompanies this article).
- Restart IIS again.
Now, you need to create the callout assembly. To do so, follow these ten steps.
- Open Visual Studio and create a new class library named LeadIntegrationCRMCallout.
- Copy the Microsoft.Crm.Platform.Callout.Base.dll located in the Microsoft CRM server CD (Disk 1) at <server CD>\GAC to your local bin folder.
- Add a reference to that DLL.
- Add a web service reference to the CRM service and name it CrmSdk.
- Rename the default class1.cs to LeadCallout.cs.
- Paste (or download) the code shown below into LeadCallout.cs
public override void PostUpdate(
CalloutUserContext userContext,
CalloutEntityContext entityContext,
string preImageEntityXml,
string postImageEntityXml)
{
XmlDocument entityDoc;
try
{
entityDoc = new XmlDocument();
entityDoc.LoadXml(postImageEntityXml);
string nameSpaceValue = entityDoc.ChildNodes[0].Attributes
["xmlns"].Value;
XmlNamespaceManager xsn = new
XmlNamespaceManager(entityDoc.NameTable);
xsn.AddNamespace("z," nameSpaceValue);
string entityXpath = "//z:BusinessEntity";
XmlNode businessEntityNode =
entityDoc.SelectSingleNode(entityXpath, xsn);
string businessEntityName =
((XmlElement)businessEntityNode).
GetAttribute("Name").ToString();
switch (businessEntityName.Trim().ToLower().ToString())
{
case MSCRMLEAD:
ProcessLeads(preImageEntityXml, postImageEntityXml);
break;
default:
break;
}
}
catch (Exception ex)
{
//TODO :Log exceptions to Event log
}
finally
{
entityDoc = null;
}
}
To finish the procedure:
- Build the assembly.
- Restart IIS.
- Copy the newly created assembly into the C:\Program Files\Microsoft CRM\server\bin\assembly folder.
- Restart IIS.