Create an Import File for Data Migration
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
Find the latest SDK documentation: CRM 2015 SDK
The following code example shows how to create an import file entity.
This sample code can be found in the following files in the SDK download:
Server\HowTo\CS\DataMigration\CreateImportFile.cs
For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.
Example
[C#]
using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
namespace Microsoft.Crm.Sdk.HowTo.DataMigration
{
/// <summary>
/// This sample shows how to create an importfile entity.
/// </summary>
public class CreateImportFile
{
public CreateImportFile()
{
}
public static bool Run(string crmServerUrl, string orgName)
{
bool success = true;
try
{
// Set up the CRM Service.
CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
service.PreAuthenticate = true;
#region Setup Data Required for this Sample
#endregion
// Create a data map that the "import" entity will use to map the data in the "importfile" to a
// Microsoft Dynamics CRM entity.
importmap importMap = new importmap();
// Set the data map properties.
importMap.name = "Data map from SDK";
importMap.source = "Import map source name";
importMap.description = "Import map source description";
// Create the map.
Guid importMapId = service.Create(importMap);
// Create a column mapping for the account name 'text' field.
columnmapping colMapping1 = new columnmapping();
colMapping1.importmapid = new Lookup(EntityName.importmap.ToString(), importMapId);
colMapping1.processcode = new Picklist(ImportProcessCode.Process);
colMapping1.sourceattributename = "COL1";
colMapping1.sourceentityname = "Account";
colMapping1.targetattributename = "name";
colMapping1.targetentityname = EntityName.account.ToString();
Guid colMappingId1 = service.Create(colMapping1);
// Create a column mapping for the account city 'text' field.
columnmapping colMapping2 = new columnmapping();
colMapping2.importmapid = new Lookup(EntityName.importmap.ToString(), importMapId);
colMapping2.processcode = new Picklist(ImportProcessCode.Process);
colMapping2.sourceattributename = "COL2";
colMapping2.sourceentityname = "Account";
colMapping2.targetattributename = "address1_city";
colMapping2.targetentityname = EntityName.account.ToString();
Guid colMappingId2 = service.Create(colMapping2);
// Create a column mapping for the account state 'text' field.
columnmapping colMapping3 = new columnmapping();
colMapping3.importmapid = new Lookup(EntityName.importmap.ToString(), importMapId);
colMapping3.processcode = new Picklist(ImportProcessCode.Process);
colMapping3.sourceattributename = "COL3";
colMapping3.sourceentityname = "Account";
colMapping3.targetattributename = "address1_stateorprovince";
colMapping3.targetentityname = EntityName.account.ToString();
Guid colMappingId3 = service.Create(colMapping3);
// Create an import entity.
import import = new import();
// Set the import properties.
import.isimport = new CrmBoolean(true);
import.name = "SDK import data sample";
import.modecode = new Picklist(ImportModeCode.Create);
// Create the import entity.
Guid importId = service.Create(import);
// Create a logical file for data import.
importfile aFile = new importfile();
// Set the file properties.
aFile.name = "SDK import file sample";
aFile.source = "name of the source file";
aFile.sourceentityname = EntityName.account.ToString();
aFile.targetentityname = EntityName.account.ToString();
// The first row of the data is not a header (see content property that follows).
aFile.isfirstrowheader = new CrmBoolean(false);
aFile.headerrow = "COL1, COL2, COL3";
aFile.additionalheaderrow = string.Empty;
// Do not import duplicate records.
aFile.enableduplicatedetection = new CrmBoolean(true);
// Interpret any double quotes as data seperators.
aFile.datadelimitercode = new Picklist(ImportDataDelimiter.DoubleQuote);
// The data is comma-separated.
aFile.fielddelimitercode = new Picklist(ImportFieldDelimiter.Comma);
// Set relationships with the import and importmap entities.
aFile.importmapid = new Lookup(EntityName.importmap.ToString(), importMapId);
aFile.importid = new Lookup(EntityName.import.ToString(), importId);
// This is the raw data for the import. This is simulating what would usually be
// read from a file on disk. Note that the linebreak is required to differentiate between
// the first and second rows of data.
aFile.content = @"accountName1, accountCity1, accountState1
accountName2, accountCity2, accountState2";
// Create the import file.
Guid fileId = service.Create(aFile);
#region check success
// Validate that the importfile entity was created.
// Be aware that using AllColumns may adversely affect
// performance and cause unwanted cascading in subsequent
// updates. A best practice is to retrieve the least amount of
// data required.
importfile createdFile = (importfile)service.Retrieve(EntityName.importfile.ToString(), fileId, new AllColumns());
if (createdFile.statecode.Value != ImportFileState.Active)
{
success = false;
}
#endregion
#region Remove Data Required for this Sample
// Delete the importfile.
service.Delete(EntityName.importfile.ToString(), fileId);
// Delete the importmap.
service.Delete(EntityName.importmap.ToString(), importMapId);
// Delete the import.
service.Delete(EntityName.import.ToString(), importId);
#endregion
}
catch (System.Web.Services.Protocols.SoapException)
{
// Perform error handling here.
throw;
}
catch (Exception)
{
throw;
}
return success;
}
}
}
See Also
Concepts
Reference
.gif)