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);
}
}
}
}