To store data into CRM application, first, we need to connect CRM service by invoking organization service proxy.
Follow the below simple steps to connect and insert records into dynamics CRM application from C# code.
Step 1:
Import the below DLLS into your application from Microsoft Dynamics CRM SDK bin folder.
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
Step 2:
Add below code into your custom C# application
private object getServiceProxcy()
{
String CRMURL = "http://mycrmserver:5555/TestOrg/XRMServices/2011/Organization.svc";
Uri OrganizationUri = new Uri(CRMURL);
ClientCredentials ClientCredentials
= new
ClientCredentials();
System.Net.NetworkCredential
network = new System.Net.NetworkCredential
{
UserName = "administrator",
Password = "admin1234@",
Domain = "mydomain"
};
ClientCredentials.Windows.ClientCredential = network;
OrganizationServiceProxy
serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, ClientCredentials, null);
return serviceProxy;
}
Step 3:
Let’s assume that, we have to insert an account record into CRM application when button clicked in C# custom application.
For this add below code inside button click event.
protected void btnInsert_Click(object sender, EventArgs e)
{
OrganizationServiceProxy serviceProxy = this.getServiceProxcy() as
OrganizationServiceProxy;
Entity entity = new Entity("account");
entity.Attributes["name"] = "Test
Account";
entity.Attributes["emailaddress"] = “test@test.com";
entity.Attributes["phone"] = "123456";
serviceProxy.Create(entity);
//This will send the record to CRM
}
Comments
My web page ...