There was ask form me to check, whether we can post data to CRM 2013 Via InfoPath 2013 form. I was aware that we can call a Custom WCF service which call CRM service and post the data to CRM from InfoPath form.

I was wondering whether we can use the custom code in the InfoPath to post the data to CRM. I did a small POC and found that it’s possible to post data using XRM sdk from InfoPath form

Here are the steps to follow

Step 1

Create an InfoPath Form

Pic10

Step2:

Go to File menu à Submit Options. In that select the option as shown in the Pic below

Pic11

 

Step 3:

Click on the Edit code and VS Will open and add the below code in the FormEvents_Submit

var Contact = new Entity("Contact");
Contact.Attributes["name"] = "Test from crm";
Uri oUri= new Uri("https://<Orgname>.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
//** Your client credentials
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = <<username>;
clientCredentials.UserName.Password = <<Password>>;

//Create your Organization Service Proxy
OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(oUri,null,clientCredentials,null);

_serviceProxy.Create(Contact);

And also refer the below XRm Dll’s

using Microsoft.Xrm.Sdk; (Microsoft.Xrm.Sdk.dll)
using Microsoft.Xrm.Sdk.Client; (Microsoft.Xrm.Sdk.dll)
using System.ServiceModel.Description; (System.ServiceModel.dll)

Build the code and close the VS

 

Step 4:

Give Full trust permission to the form. Go to file menu à Form Options.

Follow as per below diagram

Pic12

Save the form and preview the form. And submit it, you can see the record is created in the CRM.

Happy Coding

 

Elam