Quick Inquiry

Home >> Articles >> How to call WebAPI from Microsoft dynamics CRM custom activity

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.

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.

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).

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

That’s it.

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 info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved