Read the .csv format excel from Console and create records in CRM
Read the .csv format excel from Console and create records in CRM
In this post, I will explain how to read the csv file from console and insert the records into MS Crm.
For that first prepare the Excel file like below. and save as .CSV
Upload into your project for better execution.
My file name is All Contacts M.csv- Create console Application in visual studio
- open the solution explorer and right click on proejct and go to manage new get package
- Download the LINGtoCSV and also below package to project
Create a Model class with the varables like below
use the below namespace after download the package
using LINQtoCSV;
using System;
[Serializable]
class CsveFilednames
{
[CsvColumn (Name = "First Name", FieldIndex =1)]
public String Fistname { get; set; }
[CsvColumn(Name = "Last Name", FieldIndex = 2)]
public string Lastname { get; set; }
[CsvColumn(Name = "Email", FieldIndex = 3)]
public string email { get; set; }
[CsvColumn(Name = "Business Phone", FieldIndex = 4)]
public string Phonenumber { get; set; }
}
in the Csv column name pass the same name as csv column name with index
for Mani class method add the below reference to work on it
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using LINQtoCSV;
- Upload the created Csv file to proejct
- Create one static method for the csv file property
public static void Readingdata()
{
var Description = new CsvFileDescription()
{
// informing that fist row contains column names
FirstLineHasColumnNames = true,
//Use the index to read the data in csv file
UseFieldIndexForReadingData = true,
//If any unknow column is there then igonre it
IgnoreUnknownColumns =true,
//Mention type of character for the each data
SeparatorChar=','
};
}
- We need to connect the crm using appid and user name and password of the crm instacne with URL
- Create connection string by passing the userid, password, url and Appid.
static string Url = "https://Crm instance url/main.aspx";
static string Username = "Username";
static string Password = "Password";
Appid as any model driven app id
static string connectionstring = $@" AuthType=OAuth;Url={Url};UserName={Username};Password={Password};
AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;Redirecturi= http://localhost;LoginPrompt=Auto;RequireNewInstance=True";
- by using crmserviceclient create the connection by passing the connection string
CrmServiceClient Coone = new CrmServiceClient(connectionstring);
{
// I am checking that connection is ready or not
if (Coone.IsReady)
{
WhoAmIResponse response =
(WhoAmIResponse)Coone.Execute(new WhoAmIRequest());
IOrganizationService service = (IOrganizationService)Coone.OrganizationWebProxyClient != null ? Coone.OrganizationWebProxyClient :
(IOrganizationService)Coone.OrganizationServiceProxy;
CsvContext csvContext = new CsvContext();
var csveFilednames = csvContext.Read<CsveFilednames>(@"Pass the csv file path");
//by using for each we are reading csv file records
foreach (var Contactdata in csveFilednames)
{
// I am creating Entity object to create record
Entity Contactonject = new Entity("contact");
Contactonject["firstname"] = Contactdata.Fistname;
Contactonject["lastname"] = Contactdata.Lastname;
Contactonject["emailaddress1"] = Contactdata.email;
Contactonject["telephone1"] = Contactdata.Phonenumber;
Guid Rcrodid= service.Create(Contactonject);
Console.WriteLine(Rcrodid);
}
Comments
Post a Comment