为Dynamics 365写一个简单程序实现解决方案一键迁移
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复258或者20170627可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me 。
在我们做项目的过程中,一般会涉及到多个Dynamics 365环境,一般包括一个开发环境、一个SIT环境,一个UAT环境和一个生产环境,经常涉及到解决方案从开发环境迁移到SIT环境,从开发环境迁移到UAT环境,从开发环境迁移到生产环境等等。一般手工操作是先更改解决方案版本,保存后发布解决方案,再导出解决方案,再导入解决方案到目标环境。一个解决方案还好,解决方案多了麻烦,容易手误或者漏操作,可以写个程序来做这些繁杂的事情吗?Follow me。
直接上代码,代码中有注释说明,注意我这里是导入到同一个环境,你使用我的代码的时候要改动到其他CRM环境,我这里的示例是迁移一个解决方案包,项目中很一般是迁移多个。我这里导出解决方案是放在电脑的下载文件夹中:
1 public static IServiceManagement<IOrganizationService> sm; 2 static void Main(string[] args) 3 { 4 try 5 { 6 sm = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://demo.luoyong.me/XRMServices/2011/Organization.svc")); 7 ClientCredentials credentials = new ClientCredentials(); 8 credentials.UserName.UserName = "crmadmin@luoyong.me"; 9 credentials.UserName.Password = "Pass"; 10 using (var _serviceProxy = new OrganizationServiceProxy(sm, credentials)) 11 { 12 _serviceProxy.Timeout = new TimeSpan(0, 20, 0);//默认为两分钟,这里设置为20分钟 13 _serviceProxy.EnableProxyTypes(); 14 Console.WriteLine("Dynamics 365中可见的解决方案列表:" + DateTime.Now.ToLongTimeString()); 15 QueryExpression qe = new QueryExpression("solution"); 16 qe.ColumnSet = new ColumnSet("uniquename", "friendlyname", "version", "solutionpackageversion", "ismanaged"); 17 qe.Criteria.AddCondition("isvisible", ConditionOperator.Equal, true); 18 qe.AddOrder("uniquename", OrderType.Ascending); 19 var solutions = _serviceProxy.RetrieveMultiple(qe); 20 foreach(var item in solutions.Entities) 21 { 22 Console.WriteLine(string.Format("uniquename={0};friendlyname={1};version={2};solutionpackageversion={3};ismanaged={4}", 23 item.GetAttributeValue<string>("uniquename"), 24 item.GetAttributeValue<string>("friendlyname"), 25 item.GetAttributeValue<string>("version"), 26 item.GetAttributeValue<string>("solutionpackageversion"), 27 item.GetAttributeValue<bool>("ismanaged"))); 28 Console.WriteLine(new String('-',80)); 29 } 30 Console.WriteLine("开始查询要导出的解决方案并更改版本信息" + DateTime.Now.ToLongTimeString()); 31 var toExpSolutionUniqueName = "DemoSolution"; 32 qe = new QueryExpression("solution"); 33 qe.ColumnSet = new ColumnSet("version"); 34 qe.Criteria.AddCondition("uniquename", ConditionOperator.Equal, toExpSolutionUniqueName); 35 qe.TopCount = 1; 36 solutions = _serviceProxy.RetrieveMultiple(qe); 37 if(solutions.Entities.Count >= 1) 38 { 39 var solution = solutions.Entities[0]; 40 solution["version"] = string.Format("8.2.{0}.{1}", DateTime.Now.Month, DateTime.Now.Day); 41 _serviceProxy.Update(solution); 42 } 43 Console.WriteLine("开始发布所有自定义项" + DateTime.Now.ToLongTimeString()); 44 PublishAllXmlRequest pubReq = new PublishAllXmlRequest(); 45 _serviceProxy.Execute(pubReq); 46 Console.WriteLine("开始导出" + DateTime.Now.ToLongTimeString()); 47 ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); 48 exportSolutionRequest.Managed = false; 49 exportSolutionRequest.SolutionName = toExpSolutionUniqueName; 50 exportSolutionRequest.TargetVersion = "8.2";//Dynamics 365导出时候可以选择目标环境用什么版本 51 ExportSolutionResponse exportSolutionResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportSolutionRequest); 52 byte[] exportXml = exportSolutionResponse.ExportSolutionFile; 53 string filename = string.Format("{0}_{1}.zip", toExpSolutionUniqueName, solutions.Entities[0].GetAttributeValue<string>("version").Replace('.','_')); 54 File.WriteAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).Replace("Documents", "Downloads") + "\\" + filename, exportXml); 55 Console.WriteLine("开始导入" + DateTime.Now.ToLongTimeString()); 56 byte[] fileBytes = File.ReadAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).Replace("Documents", "Downloads") + "\\" + filename); 57 ImportSolutionRequest impReq = new ImportSolutionRequest() 58 { 59 CustomizationFile = fileBytes, 60 PublishWorkflows = true 61 }; 62 _serviceProxy.Execute(impReq); 63 Console.WriteLine("开始发布所有自定义项" + DateTime.Now.ToLongTimeString()); 64 pubReq = new PublishAllXmlRequest(); 65 _serviceProxy.Execute(pubReq); 66 Console.WriteLine("程序运行成功!"); 67 Console.ReadKey(); 68 } 69 } 70 catch (FaultException ex) 71 { 72 Console.WriteLine("程序出现异常:ex.Message=" + ex.Message); 73 Console.ReadKey(); 74 } 75 }
展示效果如下图:展示效果如下图:
可能你会对PublishAllXmlRequest这个消息产生疑问,这个是发布哪个解决方案的所有自定义项,实验证明应该是发布所有解决方案的所有自定义项。在导出解决方案之前请执行下这个消息确保所有的自定义项都发布了,在导入解决方案之后也执行下这个消息导入后的解决方案生效。
Dynamics 365的一个新增功能是导出解决方案时候可以指定解决方案的版本,可以选择 8.0, 8.1或者8.2,我这个导出程序使用了默认的 8.2.
值得说明的是高级查找并不能查询解决方案这个实体,所以我这里用的是QueryExpression这种查询方法,而不是使用FetchXml来查询。当然两者是可以互相转换的。查看这个实体的元数据还是使用Dynamics 365提供的Metadata Browser这个解决方案吧。
导入解决方案可以使用异步操作,请参考:Use ExecuteAsync to execute messages asynchronously 。示例如下:
string ManagedSolutionLocation = @"C:\temp\ManagedSolutionForImportExample.zip"; byte[] fileBytes = File.ReadAllBytes(ManagedSolutionLocation); ImportSolutionRequest impSolReq = new ImportSolutionRequest() { CustomizationFile = fileBytes }; ExecuteAsyncRequest asyncReq = new ExecuteAsyncRequest() { Request = impSolReq }; var asyncResp = (ExecuteAsyncResponse)svc.Execute(asyncReq); Guid asyncOperationId = asyncResp.AsyncJobId;