标签:value 实现 export proxy rgs admin val ack 开发环境
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@word10"; 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 }; 61 _serviceProxy.Execute(impReq); 62 Console.WriteLine("开始发布所有自定义项" + DateTime.Now.ToLongTimeString()); 63 pubReq = new PublishAllXmlRequest(); 64 _serviceProxy.Execute(pubReq); 65 Console.WriteLine("程序运行成功!"); 66 Console.ReadKey(); 67 } 68 } 69 catch (FaultException ex) 70 { 71 Console.WriteLine("程序出现异常:ex.Message=" + ex.Message); 72 Console.ReadKey(); 73 } 74 }
展示效果如下图:展示效果如下图:
为Dynamics 365写一个简单程序实现解决方案一键迁移
标签:value 实现 export proxy rgs admin val ack 开发环境
原文地址:http://www.cnblogs.com/luoyong0201/p/Export_Import_Solution_By_One_Click.html