How to call WebAPI from Microsoft dynamics CRM custom activity

banner

It was yesterday when I and one of my colleague was working on Microsoft Dynamics CRM and we stuck into a situation where we did not know how to call WebAPI method from CRM custom activity. After many efforts and trials, we found the way for it and now, we are sharing it in this post.

How to Call WebAPI Method

Here, I will explain how to call WebAPI method from CRM Custom activity and get Invoice from XERO.

For this, I created account on XERO, and created one WebAPI method to get invoice from that XERO account. After creating account on XERO I get Consumer key which will required to call WebAPI method.

This code required Url which will call webAPI method to get or create invoice on XERO. You will get this Url from your XERO account.

Now create a custom activity project and create one custom activity in that. Set string as output parameter, because below code will return the formatted string which contains Invoice ID and Invoice Number. You can set Invoice Entity as output parameter because below code mainly return Invoice from XERO. I just format Invoice ID and Invoice Number as string for better understanding.

Creating Invoice Class in Custom Activity

Now, create Invoice class in custom activity with below fields. You can create Invoice class more than these fields same as response object.

publicclassInvoice { publicGuidInvoiceID { get; set; } publicstringInvoiceNumber { get; set; } publicstringCurrencyCode { get; set; } publicstring Status { get; set; } publicstring Reference { get; set; } publicdecimal Total { get; set; } publicDateTimeXeroInoviceDate { get; set; } publicstringXeroContactName { get; set; } }

Next, create object for Invoice class and WebClient class in your custom activity as below. This is also a simple code to call WebAPI and get invoice from XERO account by InvoiceID. I am using WebClient.DownloadString to call WebAPI.

The Url passed in DownloadString to get Invoice from XERO contains four things.

  • XERO API root url (http://{Web API URL}/xero).
  • WebAPI method name (In my case this is “findinvoice” butyou can use default methods of XERO).
  • XERO Consumer Key.
  • And last is the Invoice ID (for this Invoice ID, create one Invoice direct into XERO).
crm-development

Now, create workflow to call this custom activity, and set response text to any string field on CRM entity form

Worried about calling WebAPI method from CRM custom activity?

Approach us to get a suitable solution for the query as the solution is explained in easy steps.

Conclusion

Hope you learnt the method of calling WebAPI from Microsoft Dynamics CRM Development custom activity. You can share your reviews for this post and your experience with CRM system. For more updates related to CRM system, keep looking for more of such posts from us.

The same code is useful to call other WEB APIs from the Custom Activity too with slight change on processing output. You can read your object from response memory stream instead.

Hope that helps.

For further information, mail us at [email protected]

Related article

Update Rollup 21 for Microsoft Dynamics CRM 4.0 is available. The update is available in multiple languages to help international users.

Aegis Softtech team is sharing this news with CRM users and development community.

Good news for asp.net developers and researchers is that Web API 2.1, MVC 5.1, and Web Pages 3.1 for asp.net development are live now on

DMCA Logo do not copy