Import & Export Microsoft Dynamics CRM Customizations as XML or as compressed file programatically (dynamically) using CRM API/Web Service

 

It is very common that you would need to import customizations from a file and upload it directly on CRM then publish thes customizations OR the opposite: where you import customisations from Dynamic CRM implementation and export to a file either XML file or a Zip file which you can then archive or add to your source control (TFS or Source Safe). The later is specially common when you need to have a regular backup of your customizations that is stored in TFS or your choses Source control.

To do this, MSDN provides several ways of importing and exporting files (see: http://msdn.microsoft.com/en-us/library/cc151164.aspx)

This is what you can do:

Message name Description
ExportAllXml Export all customizations to an XML file.
ExportCompressedAllXml Export all customizations to an XML file, which is then compressed into a zipped format.
ExportCompressedTranslationsXml Export all translations to an XML file, which is then compressed into a zipped format.
ExportCompressedXml Export the specified customizations to an XML file, which is then compressed into a zipped format.
ExportTranslationsXml Export all translations to an XML file.
ExportXml Export a list of entities to an XML file.
ImportAllXml Import all customizations from an XML file.
ImportCompressedAllXml Import all customizations from an XML file that has been compressed into a zipped format.
ImportCompressedXmlWithProgress Import all customizations with progress logging from an XML file that has been compressed into a zipped format.
ImportTranslationsXmlWithProgress Import all translations from an XML file with progress logging.
ImportXml Import a list of entities from an XML file.
ImportXmlWithProgress Import the specified set of entity customizations from an XML file with progress logging.
PublishAllXml Publish all customizations.
PublishXml Publish a list of entity customizations.

 

Export All XML customizations is usually the most popular one. Here is how you can do it (Code from MSDN):

// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";

CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the request.
ExportAllXmlRequest request = new ExportAllXmlRequest();

// Execute the request.
ExportAllXmlResponse response = (ExportAllXmlResponse)service.Execute(request);

// Load the results into an XML document.
XmlDocument entityXml = new XmlDocument();
entityXml.LoadXml(response.ExportXml);

 

As for Import All XML customization followed by a publish, here is the code for it:

// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";

CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
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; 

// Create the request.
ExportAllXmlRequest request = new ExportAllXmlRequest();

// Execute the request.
ExportAllXmlResponse exportResponse = (ExportAllXmlResponse)service.Execute((Request)request);

// Create the request.
ImportAllXmlRequest importRequest = new ImportAllXmlRequest();

// Tell the request where the temporary XML file is located.
importRequest.CustomizationXml = exportResponse.ExportXml;

// Execute the import.
ImportAllXmlResponse importResponse = (ImportAllXmlResponse)service.Execute(importRequest);

Finally to publish your customizations, this is what you need to do:

// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";

CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create the request.
PublishAllXmlRequest request = new PublishAllXmlRequest();

// Execute the request.
PublishAllXmlResponse response = (PublishAllXmlResponse)service.Execute(request);

One Reply to “Import & Export Microsoft Dynamics CRM Customizations as XML or as compressed file programatically (dynamically) using CRM API/Web Service”

Please comment or leave feedback