Creating a simple Callout in Microsoft Dynamics CRM 3.0

Create a class library project in Visual Studio 2003.

 

Add reference to the following assembly Microsoft.Crm.Platform.Callout.Base.dll.

 

Inherit the Microsoft.Crm.Callout.CrmCalloutBase class.

 

Now override the event against which you want to put your business logic.

 

 

Add web reference to CrmService

http://servername:port/mscrmservices/2006/crmservice.asmx

 

Set up the CrmService properties like url, credentials and CallerIdValue

 

CrmService service = new CrmService();

            service.Url = “http://servername: port /mscrmservices/2006/CrmServiceWsdl.aspx”;

            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            service.CallerIdValue = new CallerId();           

            service.CallerIdValue.CallerGuid = userContext.UserId;

 

For our PostUpdate event handler we can see that the values for preImageEntityXml as well as postImageEntityXml are passed as a string which essentialy is a xml.

 

public override void PostUpdate(

                                                CalloutUserContext  userContext,

                                                CalloutEntityContext  entityContext,

                                                string  preImageEntityXml,

                                                string  postImageEntityXml

                                                )

 

So first we will convert it into a dynamic entity

 

DynamicEntity entityPost=ConvertToDynamicEntity(postImageEntityXml);

 

The definition of the ConvertToDynamicEntity function is

 

private static DynamicEntity ConvertToDynamicEntity(string xml)

                                {

 

                                                TextReader sr = new StringReader(xml);

 

                                                XmlRootAttribute root = new XmlRootAttribute(“BusinessEntity”);

                                                root.Namespace = http://schemas.microsoft.com/crm/2006/WebServices”;

                                               

 

                                                XmlSerializer xmlSerializer = new XmlSerializer(typeof(BusinessEntity), root);

                                                BusinessEntity entity = (BusinessEntity)xmlSerializer.Deserialize(sr);

 

                                                return (DynamicEntity) entity;

                                }

 

We’ll deserialize the xml output to get an instance of DynamicEntity.

 

To make it more easy to work with the properties we can make use of the following class PropertiyDictionary in our callout.

 

https://nishantrana.files.wordpress.com/2009/02/propertydictionary.doc

 

 

We’ll pass the instance of the newly created dynamic entity to the PropertyDictionary’s constructor.

 

PropertyDictionary properties=new PropertyDictionary(entityPost);

 

Now to work with properties we can do something like this

 

// For CrmDateTimeProperty

            if (properties.Contains(“actualclosedate”))

            {

                CrmDateTimeProperty acd = (CrmDateTimeProperty)properties[“actualclosedate”];

                actualclosedate = acd.Value;

            }

 

            // For CrmMoneyProperty

            if (properties.Contains(“new_estimatedrevenue”))

            {

 

                CrmMoneyProperty ner = (CrmMoneyProperty)properties[“new_estimatedrevenue”];

                new_estimatedrevenue = ner.Value;

            }

            // For PicklistProperty

            if (properties.Contains(“salesstagecode”))

            {

 

 

                PicklistProperty salesstagecode = (PicklistProperty)properties[“salesstagecode”];               

                salesstage = salesstagecode.Value.Value.ToString();

            }

           

 

That’s it…


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s