Plug-In Sample to Synchronize Customization between Organizations

 

Hi all,

Here is a new Plug-In Sample proposition from my colleague Fouad Rachkidi:

- Scenario is having 2 or more organizations

- These organizations can be for testing, development, acceptance…

- You make schema customization on 1 organization and once you select “Publish All Customizations” the plug-in will be fired

- The plug-in will make a backup of the customization using the export message and a copy will be saved to disk (You can also make the backup before the publish is done Pre-PublishAll message)

- The exported file will be imported into a different organization using the import message

- Eventually both organizations will have the same schema, the plug-in is synchronizing any schema changes between the organizations.

Plug-In Registration Details:

 <Register LogFile="Plug-in Registration Log.txt" Server="https://sqlcrmsrv:5555" Org="Acceptance" Domain="frachkid" UserName="crmadmin">
   <Solution SourceType="1" Assembly="ExportPublishImport.dll" Id="f6ec33d5-18f5-45d2-995c-bba5fda12443">
     <PluginTypes>
       <Plugin TypeName="ExportPublishImport.PluginClass" FriendlyName="c0f48c56-eeb5-48da-9bf0-c435ad4ddd14" Id="531f2c4a-7843-4bbe-af0d-33148bfea8a2">
         <Steps>
           <Step PluginTypeName="ExportPublishImport.PluginClass" PluginTypeFriendlyName="c0f48c56-eeb5-48da-9bf0-c435ad4ddd14" CustomConfiguration="" SecureConfiguration="" Description="PublishAll of none in Parent Pipeline" FilteringAttributes="" ImpersonatingUserId="" InvocationSource="0" MessageName="PublishAll" Mode="0" PrimaryEntityName="none" SecondaryEntityName="none" Stage="50" SupportedDeployment="0" Rank="1" Id="3ca8b544-bfce-de11-864c-00155dcd1a24">
             <Images />
           </Step>
         </Steps>
       </Plugin>
     </PluginTypes>
   </Solution>
 </Register>

Plug-In Source Code:

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Web.Services.Protocols;
 using System.IO;
  
 using Microsoft.Crm.Sdk;
 using Microsoft.Crm.SdkTypeProxy;
  
 using ExportPublishImport.ProductionCrmService;
  
 namespace ExportPublishImport
 {
     //Message: PublishAll
     //Primary Entity: None
     //Stage: Post 
     //Mode: Synchronous
     //Pipeline: Parent
     public class PluginClass : IPlugin
     {
         public void Execute(IPluginExecutionContext context)
         {
             try
             {
                 if (context.MessageName.Equals("PublishAll"))
                 {
                     //Exporting all customizations from test organization
                     ICrmService service = context.CreateCrmService(true);
                     Microsoft.Crm.SdkTypeProxy.ExportCompressedAllXmlRequest exportRequest = 
                         new Microsoft.Crm.SdkTypeProxy.ExportCompressedAllXmlRequest();
                     exportRequest.EmbeddedFileName = "All_CRM_Customizations.xml";
  
                     Microsoft.Crm.SdkTypeProxy.ExportCompressedAllXmlResponse exportResponse =
                         (Microsoft.Crm.SdkTypeProxy.ExportCompressedAllXmlResponse)
                         service.Execute(exportRequest);
  
                     byte[] compressedXml = exportResponse.ExportCompressedXml;
  
                     //Saving a backup copy 
                     using (FileStream fs = new FileStream(@"C:\Program Files\Microsoft Dynamics CRM\Customizations_Backup\AllCrmCustomizations.zip", FileMode.Create))
                     {
                         fs.Write(compressedXml, 0, compressedXml.Length);
                         fs.Close();
                     }
  
                     //Importing customization into a different organization
                     ProductionCrmService.CrmAuthenticationToken token = new ProductionCrmService.CrmAuthenticationToken();
                     token.AuthenticationType = 0;
                     token.OrganizationName = "Production";
  
                     ProductionCrmService.CrmService crmService = new ProductionCrmService.CrmService();
                     crmService.CrmAuthenticationTokenValue = token;
                     crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                     crmService.Timeout = 1000000;
  
                     ProductionCrmService.ImportCompressedAllXmlRequest importRequest =
                         new ProductionCrmService.ImportCompressedAllXmlRequest();
  
                     importRequest.CompressedCustomizationXml = compressedXml;
  
                     ProductionCrmService.ImportCompressedAllXmlResponse importResponse =
                         (ProductionCrmService.ImportCompressedAllXmlResponse) crmService.Execute(importRequest);
                 }
             }
             catch (SoapException soapex)
             {
                 throw new InvalidPluginExecutionException(soapex.Detail.InnerText);
             }
             catch (IOException ioex)
             {
                 throw new InvalidPluginExecutionException(ioex.Message);
             }
         }
     }
 }

Hope you’ll find this useful.

Regards

-Ben