Microsoft Dynamics 365 Blog

In This Post:

  • Problem statement
  • How to create and export CRM Data
  • How to create DataMap and save it
  • How to import sample data using SDK APIs
  • Conclusion
  • Further reading
Problem statement

You have created a solution using the Microsoft Dynamics CRM 2011 Solutions framework and you want people to download it and try it. But without some sample data it may be difficult for people to understand how it is supposed to work.

This blog describes a way to create a script to install sample data for any Microsoft Dynamics CRM 2011solution.

In order to create the sample data application you need to:

  • Create the sample data in the CRM System and export it using existing Data Export functionality.
  • Import it back in a new CRM System and create the Data Mappings
  • Export the Data Mappings
  • Use the Exported Data XML files plus the Data Mappings XML to create a C# application that can import the sample data in any Microsoft Dynamics CRM 2001 system that has our solution deployed.

Note: These steps require that you have some .NET programming experience.

How to create and export CRM Data

Let’s say you have a solution that has 2 entities: a household and a client.

For this example we will create 2 household instances and 2 client instances that we will export using Import/Export Functionality. To access the Export functionality, click Advanced Find and select, in our case, the Clients. Build the query to select the data that we want to export and launch the query as shown in the following screenshot.

Now we are ready the export the results using the Export Clients button from the Ribbon as shown in the following screenshot.

Save the export as an *.xls (Excel) file and repeat the operation for all the entities that you want to create sample data with. For this sample you would repeat it for households.

To be able to import the data using the SDK APIs, we need to transform all the xls files into xml. To do that, open each xls file in Excel, and choose save as XML Spreadsheet 2003 as shown in the following screenshot.


Now, you can compress the 2 xml files into a zip file as shown in the following screenshot.

Then you can import the zip back using the CRM Import feature with the purpose of creating the data map.


How to Create a DataMap and Save it

The next step is to create the DataMap.xml. Data maps show how to map your XML record data to Microsoft CRM entities and attributes.

To create the SampleDataMap, we will import the xml zip file data into CRM, and during that process, capture and save the DataMap created automatically by CRM. The following steps describe how:

    1. Navigate to Settings/Data Management/ Imports

    2. Click Import Data from the ribbon as shown in the following screenshot.

    • In the Import Data Wizard dialog box, browse to the zip file you created earlier.

    4. Navigate the screens and by clicking next and fix all the mapping warnings :


      5. On the Review Settings and Import Data screen give a name for the Data Map Name. For this exercise we will name it “SampleDataMap”.

      6. Click Submit and now you are ready to export the data map xml.

      7. Navigate to Settings/Data Management/ Data Maps and click Export to export the xml data map as shown in the following screenshot.


      Now with all these xml files (entities XML + data map XML), we are ready to us the SDK to import the data in any system that has our solution deployed.

      How to Import Sample Data using SDK APIs

      With your Client.xml, Household.xml and SampleDataMap.xml you can write a console application to import the sample data in any organization that has your Microsoft Dynamics CRM 2011 solution.

      Using the Microsoft Dynamics CRM 2011 SDK samples as source templates for the initial connection with CRM we can add additional code to import the sample data.

      First we create the import request. Note that this creation doesn’t trigger the actual import. The import will be triggered at the end by calling “ImportRecordsImportRequest” SDK message. What we need to do is the following:

      1. Create an import entity record
      2. Create DataMap record
      3. Import each sample data file xml
      4. Parse
      5. Transform
      6. Call Import records that will actually perform the import sample data.

      // create import id

      Import import = new Import();

      import.ModeCode = new OptionSetValue(0);// 0 -Excel

      import.Name = “SampleDataImport”;

      import.SendNotification = false;

      Guid importId = _serviceProxy.Create(imp);

      Once you have the import id you can create the datamap. The datamap describes what attributes in the source xml files maps to what attributes in the CRM system. You will use the ImportMappingsImportMapRequest message to import the import map.

      // create import file map

      ImportMappingsImportMapRequest importMapReq = new ImportMappingsImportMapRequest();

      importMapReq.MappingsXml = File.ReadAllText(@”C:\SampleDataImportMap.xml”);

      importMapReq.ReplaceIds = true;

      ImportMappingsImportMapResponse importResponse = (ImportMappingsImportMapResponse)_serviceProxy.Execute(importMapReq);

      Guid importMapId = importResponse.ImportMapId;

      With references to the importId and the importMapId we can now import our sample data xml files. We will do this by using one helper method that can be used for all the sample files that we have.

      // Create the import file 1

      CreateImportFile(importId, importMapId, @”Account.xml”, “account”, “account”);

      The method implementation is shown below:

      private void CreateImportFile(Guid importId, Guid importMapId, string filePath, string sourceEntityName, string targetEntityName)


                      ImportFile file = new ImportFile();

                      file.Content = File.ReadAllText(filePath);

                      file.Name = sourceEntityName;

                      file.FileTypeCode = new OptionSetValue(1);// excel

                      file.IsFirstRowHeader = true;

                      file.Source = System.IO.Path.GetFileName(filePath);

                      file.SourceEntityName = sourceEntityName;

                      file.ImportMapId = new EntityReference(ImportMap.EntityLogicalName, importMapId);

                      file.ImportId = new EntityReference(Import.EntityLogicalName, importId);

                      file.TargetEntityName = targetEntityName;

                      file.Size = file.Content.Length.ToString();

                      // Note: Process code 1 = “Process”

                      file.ProcessCode = new OptionSetValue(1);

                      file.UseSystemMap = true;



      Once we imported all the sample files we are ready to:

      1. Parse the data
      2. Transform the data
      3. Invoke the actual ImportRecords SDK

      // Parse the import.

      ParseImportRequest parseRequest = new ParseImportRequest();

      parseRequest.ImportId = importId;


      // Transform the import.

      TransformImportRequest transRequest = new TransformImportRequest();

      transRequest.ImportId = importId;

      TransformImportResponse transResponse = (TransformImportResponse)_serviceProxy.Execute(transRequest);

      // Create an ImportRecordsImport request

      ImportRecordsImportRequest request = new ImportRecordsImportRequest();

      // Assign the request the id of the import we want to begin

      request.ImportId = importId;

      // Execute the request.

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

      Since the import data is an asynchronous operation, it takes some time to complete. We can pull the status of the operation by using something similar with the following code:

      // Wait for the operation to complete by polling it’s state every few seconds.

      ColumnSet cols = new ColumnSet(new string[] { “statecode” });

      // We try 50 times for example

      for (int i = 0; i < 50; i++)



                      AsyncOperation op = (AsyncOperation) _serviceProxy.Retrieve(AsyncOperation.EntityLogicalName, response.AsyncOperationId, cols);

                      if (op.StateCode.Value == AsyncOperationState.Completed)






      In this blog post we have shown that creating sample data for our Microsoft Dynamics CRM 2011 solution can be made easier if we create the sample data once, export it, and use SDK APIs to create a simple console application to import the sample data in any system that has our solution deployed.

      You have created a solution using the Microsoft Dynamics CRM 2011 Solutions framework and want to include sample data with it, the next step would be to create a Silverlight Web Resource that you expose in the configuration page for your solution which will allow your customers to import a set of sample data that will make it easier for them to understand how your solution works.

       Further reading

      Creating Custom Sample Data for Microsoft Dynamics CRM 2011

      Creating Custom Sample Data for CRM 2011 – Advanced


      Doru Rotovei


      We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!