
Hello all, i am new in plugin code.
I have to write a plugin code for - uploading a appointment excel file to d365 prod environment . And mapping all the column of Appointment entity .
This all by doing code in c#
Can you give a brief example of similar
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using CsvHelper;
using System.Globalization;
using CsvHelper.Configuration; //download nudget package
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
string connectionName = "DEV_Connection";
IOrganizationService svcProxy = CRMConnection(connectionName);
if (svcProxy != null)
{
string filePath = @"C:\Appointment\Appointment6.csv"; // Replace with the path to your CSV file
string entityName = "appointment"; // Replace with the name of your Dynamics 365 CRM entity
List<Entity> recordsToCreate = FetchCsvData(filePath, entityName);
if (recordsToCreate.Count > 0)
{
foreach (Entity record in recordsToCreate)
{
try
{
Guid createdRecordId = svcProxy.Create(record);
Console.WriteLine("Record created with ID: " + createdRecordId);
}
catch (Exception ex)
{
Console.WriteLine("Error creating record: " + ex.Message);
}
}
}
else
{
Console.WriteLine("No records to create.");
}
}
Console.Read();
}
static IOrganizationService CRMConnection(string connectionName)
{
CrmServiceClient devConnection = new CrmServiceClient("AuthType=Office365;Url=**********;Username=**********;Password=*********");
IOrganizationService svcProxy = devConnection.OrganizationWebProxyClient != null ?
(IOrganizationService)devConnection.OrganizationWebProxyClient :
devConnection.OrganizationServiceProxy;
return svcProxy;
}
static List<Entity> FetchCsvData(string filePath, string entityName)
{
List<Entity> recordsToCreate = new List<Entity>();
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ",", // Set the field delimiter to a comma (,)
Quote = '"' // Set the data delimiter to a quotation mark (")
};
csv.Read();
csv.ReadHeader();
Dictionary<string, string> fieldMappings = new Dictionary<string, string>
{
{ "Start Time", "scheduledstart" }, // mapping the field
{ "End Time", "scheduledend" },
//{ "INDSKR_ACTIVITYSUBTYPE", "indskr_activitysubtype" },
{ "INDSKR_ACTIVITYTYPE", "indskr_activitytype" },
{ "INDSKR_EXTERNALID", "indskr_externalid" },
{ "INDSKR_EXTERNALSOURCE", "indskr_externalsource" },
{ "INDSKR_NOTES", "indskr_notes" },
{ "LOCATION", "location" },
//{ "OWNERID", "ownerid" },
{ "STATECODE", "statecode" },
//{ "STATUSCODE", "statuscode" },
{ "SUBJECT", "subject" },
//{ "INDSKR_TYPE" , "indskr_type" },
// { "INDSKR_SUBTYPE", "indskr_subtype" },
};
while (csv.Read())
{
Entity record = new Entity(entityName);
foreach (var field in fieldMappings)
{
string columnName = field.Key;
string fieldName = field.Value;
string value = csv.GetField(columnName);
if (!string.IsNullOrEmpty(value) && value != DBNull.Value.ToString())
{
if (columnName == "Start Time" || columnName == "End Time")
{
DateTime dateTime;
if (DateTime.TryParseExact(value, "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
record[fieldName] = dateTime;
}
else
{
Console.WriteLine("Invalid date and time format: " + value);
continue;
}
}
else
{
record[fieldName] = value;
}
}
}
recordsToCreate.Add(record);
}
}
return recordsToCreate;
}
}
}