Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • Using the CRM 3.0 Import and Export API

    Posted on December 3rd, 2006 mitch Print Print No comments

    I spent some time this weekend working on a utility that relies on the CRMService Import, Export, and Publish methods.  While the sample code shipped with the CRM SDK ( v3.0.5 ) is fairly useful, I thought I'd give you a more real world example of the ImportAllXml and ExportAllXml API methods;

    ImportAllXml

    Import all customizations from an XML file.  For more information, review the following SDK documentation. Substitute the SDK example with the following code, which reads the XML from a file.

     

    // Set up the CRM Service.
    CrmService service = new CrmService();
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // This is a potentially long running operation. The default 90-second
    // time-out might be too short.  Set the time-out to be 5 minutes.
    // This property takes milliseconds.
    service.Timeout = 300 * 1000; 
    
    using(System.IO.StreamReader reader =
             new System.IO.StreamReader("importfile.xml"))
    {
        string entityXml = reader.ReadToEnd();
        // Create the request.
        ImportAllXmlRequest importRequest = new ImportAllXmlRequest();
    
        // Tell the request where the XML is located.
        importRequest.CustomizationXml = entityXml;    // Execute the import.
        ImportAllXmlResponse importResponse =
            (ImportAllXmlResponse)service.Execute(importRequest);
    }

    ExportAllXml

    Export all customizations to an XML file.  For more information, review the following SDK documentation. Substitute the SDK example with the following code, which writes the XML to a file.

     

    //Set up the CRM Service.
    CrmService service = new CrmService();
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // Create the request.
    ExportAllXmlRequest request = new ExportAllXmlRequest();
    
    // Execute the request.
    ExportAllXmlResponse response =
        (ExportAllXmlResponse)service.Execute(request);
    
    using(System.IO.StreamWriter writer =
             new System.IO.StreamWriter("exportfile.xml"))
    {
        writer.Write(response.ExportXml);
        writer.Flush();
    }
    Customization, Dynamics CRM
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
    Loading ... Loading ...
    1,607 views