Import Data Programmatically in Microsoft Dynamics CRM 2011

Posted: October 18, 2012 in Code Snippets, CRM 2011

Import Data using C# code in CRM 2011

In my example im using XML Spreadsheet to import the data into CRM 2011.

1) Open your excel file which you need to import into CRM and save as type XML Spreadsheet 2003 format.

2) Save your xml sheet in your local drive.

3) Before importing the data into CRM, Create a DataMap so that you can use that DataMap Id to import the data.

Below is the code to import data in CRM 2011. I’m using early bound entities in my code to get the Guid of the DataMap. Add the Autogenerated cs file in your solution by generating it using crmsvcutil in the sdk bin folder.

public static void ImportToCRM(IOrganizationService service)
        {
            // Load the xml spreasheet file from your local drive using XmlDocument class
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"C:ActiveRecords.xml");

            // Create the Import
            Import import = new Import();
            import.ModeCode = new OptionSetValue(0);
            import.Name = "CustomEntityDataImport";
            import.SendNotification = false;
            Guid importId = service.Create(import);
            
            // Get the Guid of the DataMap by using the datamap name
            XrmDataContext context = new XrmDataContext(service);
            var importMap = (from imp in context.ImportMapSet
                     where imp.Name == "AccountDataMap"
                     select new { imp.Id }).FirstOrDefault();

            // Get the Mapping Set
            var importEntityMap = (from imp in context.ImportEntityMappingSet
                                   where imp.ImportMapId.Id.Equals(importMap.Id)
                      select new { imp.SourceEntityName }).FirstOrDefault();

            Guid importMapId = importMap.Id;

            // Create the ImportFile class
            ImportFile importFile = new ImportFile();

            // Add the xml document content
            importFile.Content = xmldoc.InnerXml;
            importFile.Name = "CustomEntityDataImport";
            importFile.FileTypeCode = new OptionSetValue(1);
            importFile.IsFirstRowHeader = true;
            importFile.Source = Path.GetFileName("CustomEntityDataImport.xml");
            importFile.SourceEntityName = importEntityMap.SourceEntityName.ToString();

            // Schema name of the Target entity
            importFile.TargetEntityName = "account";
            importFile.ImportMapId = new EntityReference(ImportMap.EntityLogicalName, importMapId);
            importFile.ImportId = new EntityReference(Import.EntityLogicalName, importId);
            importFile.Size = importFile.Content.Length.ToString();
            importFile.ProcessCode = new OptionSetValue(1);
            importFile.UseSystemMap = false;
            importFile.EnableDuplicateDetection = true;
            service.Create(importFile);

            ParseImportRequest parseRequest = new ParseImportRequest();
            parseRequest.ImportId = importId;
            service.Execute(parseRequest);

            TransformImportRequest transRequest = new TransformImportRequest();
            transRequest.ImportId = importId;
            TransformImportResponse transResponse = (TransformImportResponse)service.Execute(transRequest);

            ImportRecordsImportRequest request = new ImportRecordsImportRequest();
            request.ImportId = importId;

            ImportRecordsImportResponse response = (ImportRecordsImportResponse)service.Execute(request);

        }

Advertisements
Comments
  1. murali says:

    I done the same procedure but I got this error “he column heading contains an invalid combination of data delimiters.” why can you please help thankful

  2. link says:

    can you show an example of how to do an update from sdk having the excel file?

  3. Hoang says:

    Can you share this source ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s