Use CrmSvcUtil to generate the strongly types entity and datacontext classes
crmsvcutil
/connectionString:"Authentication Type=Integrated; Server=http://servername/orgname"
/out:"Xrm.cs" /namespace:"Xrm.solution"
/dataContextClassName:"xrm"
conntectionString–> specifies the connection string.
out –> determines the name of the .cs or .xml output file and whether there is one file or one per entity.To generate one file per entity omit .cs or .xml from the file name.
namespace –> to specify the namespace
dataContextClassName –> to specify the name of the dataContext class.
Add references to the following dlls in the project
- System.Data.Entity, System.Data.Services, System.Data.Services.Client.
- System.Web.Services.
- Microsoft.Crm.Sdk, Microsoft.Crm.SdkTypeProxy.dll, Microsoft.Crm.SdkTypeProxy.XmlSerializers
- Microsoft.Xrm.Client, Microsoft.Xrm.Portal, Microsoft.Xrm.Portal.Files
Add the generated Xrm.cs file to the project.
Add the connection string information in the config file of the application
<add name="CRMOnPremise" connectionString="Authentication Type=Integrated; Server=http://servername/orgname" />
Add following using Statements
using Xrm;
using Xrm.solution;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
And the code for creating a new custom entity record.
testEntity1.new_name = "Nishant";
testEntity1.new_lastname = "Rana";
myXrm.AddTonew_tests(testEntity);
myXrm.SaveChanges();
or could also be written as
// create the instace of DataContext class named xrm
var myXrm = new xrm("CRMOnPremise");
// use concept of Implicitly type local variable
// and object intializer
var testEntity = new new_test()
{
new_name = "Nishant",
new_lastname = "Rana"
};
myXrm.AddTonew_tests(testEntity);
myXrm.SaveChanges();
Similarly to update a record and delete a record we need to use
UpdateObject() and DeleteObject() function followed by call to
SaveChanges() method.
Bye.
