Create on console application, read the .csv format excel and create records in CRM
Create on console application, read the .csv format excel and create records in CRM ?
STEP 1: Open Visual studio and create a console app and name it as ConsoleConnectCRMwithCSV
STEP 2:
- Add Nuget packages
- Search xrm.sdk then select Microsoft.Crm.Sdk.Xrm.Tooling.Core assembly and install
- Next search LINQtoCSV and install
- Next search Microsoft.PowerPlatform.Dataverse.Client and install
- so Install all these libraries in ConsoleConnectCRMwithCSV.
- name the class as CreateContact then the file name is CreateContact.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LINQtoCSV;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Rest;
using Microsoft.PowerPlatform.Dataverse.Client;
namespace ConsoleConnectCRMwithCSV
{
internal class CreateContact
{
static string url = "https://orgd4f992a9.crm8.dynamics.com/main.aspx?";
static string userName = "HanuNov@EffigentSS309.onmicrosoft.com";
static string password = "H@rish098";
static string connectionString = $@"
AuthType = OAuth;
Url ={url};
UserName ={userName};
password = {password};
RequireNewInstance = True;";
static void Main(string[] args)
{
using (var service = new ServiceClient(connectionString))
{
if (service.IsReady)
{
Console.WriteLine("Connected to Dynamics 365!");
var csvFileDescription = new CsvFileDescription
{
FirstLineHasColumnNames = true,
SeparatorChar = ',',
UseFieldIndexForReadingData = false
};
var csvContext = new CsvContext();
//To get and set the value from csv file create one class student
.cs
// Take csv file path
var namerecord = csvContext.Read<student>(@"C:\Users\HariT\source\repos\ConsoleConnectCRMwithCSV\ConsoleConnectCRMwithCSV\connectCRM.csv", csvFileDescription);
foreach (var list in namerecord)
{
Entity contact = new Entity("contact");
contact["firstname"] = list.FirstName;
contact["lastname"] = list.LastName;
contact["emailaddress1"] = list.Email;
Console.WriteLine($"{list.FirstName}|{list.LastName}|{list.Email}");
service.Create(contact);
}
}
else
{
Console.WriteLine($"Failed to connect: {service.LastError}");
}
}
}
}
}
STEP 3:
Add newitem As student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LINQtoCSV;
namespace ConsoleConnectCRMwithCSV
{
[Serializable]
public class student
{
[CsvColumn(Name = "FirstName", FieldIndex = 1)]
public string FirstName { get; set; }
[CsvColumn(Name = "LastName", FieldIndex = 2)]
public string LastName { get; set; }
[CsvColumn(Name = "Email", FieldIndex = 3)]
public string Email { get; set; }
}
}
STEP 4:
Add a file connectCRM.csv then keep the following items in csv and save it
FirstName,LastName,Email
harish,kumar,hari@gmail.com
ramu,sri,sriram@gmail.com
siddhanth,malhothra,malhothra@gmail.com
Open connectCRM.csv in folder then it will Show in Excel format
.png)
.png)

Comments
Post a Comment