关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复258或者20170627可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me 。

        在我们做项目的过程中,一般会涉及到多个Dynamics 365环境,一般包括一个开发环境、一个SIT环境,一个UAT环境和一个生产环境,经常涉及到解决方案从开发环境迁移到SIT环境,从开发环境迁移到UAT环境,从开发环境迁移到UAT环境等等。一般手工操作是先更改解决方案版本,保存后发布解决方案,再导出解决方案,再导入解决方案到目标环境。一个解决方案还好,解决方案多了麻烦,容易手误或者漏操作,可以写个程序来做这些繁杂的事情吗?Follow me。

       直接上代码,代码中有注释说明,注意我这里是导入到同一个环境,你使用我的代码的时候要改动到其他CRM环境,我这里的示例是迁移一个解决方案包,项目中很一般是迁移多个。我这里导出解决方案是放在电脑的下载文件夹中:

        public static IServiceManagement<IOrganizationService> sm;        static void Main(string[] args)        {            try            {                sm = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://demo.luoyong.me/XRMServices/2011/Organization.svc"));                ClientCredentials credentials = new ClientCredentials();                credentials.UserName.UserName = "crmadmin@luoyong.me";                credentials.UserName.Password = "Pass";                using (var _serviceProxy = new OrganizationServiceProxy(sm, credentials))                {                    _serviceProxy.Timeout = new TimeSpan(0, 20, 0);//默认为两分钟,这里设置为20分钟                    _serviceProxy.EnableProxyTypes();                    Console.WriteLine("Dynamics 365中可见的解决方案列表:" + DateTime.Now.ToLongTimeString());                    QueryExpression qe = new QueryExpression("solution");                    qe.ColumnSet = new ColumnSet("uniquename", "friendlyname", "version", "solutionpackageversion", "ismanaged");                    qe.Criteria.AddCondition("isvisible", ConditionOperator.Equal, true);                    qe.AddOrder("uniquename", OrderType.Ascending);                    var solutions = _serviceProxy.RetrieveMultiple(qe);                    foreach(var item in solutions.Entities)                    {                        Console.WriteLine(string.Format("uniquename={0};friendlyname={1};version={2};solutionpackageversion={3};ismanaged={4}",                            item.GetAttributeValue<string>("uniquename"),                            item.GetAttributeValue<string>("friendlyname"),                            item.GetAttributeValue<string>("version"),                            item.GetAttributeValue<string>("solutionpackageversion"),                            item.GetAttributeValue<bool>("ismanaged")));                        Console.WriteLine(new String('-',80));                    }                    Console.WriteLine("开始查询要导出的解决方案并更改版本信息" + DateTime.Now.ToLongTimeString());                    var toExpSolutionUniqueName = "DemoSolution";                    qe = new QueryExpression("solution");                    qe.ColumnSet = new ColumnSet("version");                    qe.Criteria.AddCondition("uniquename", ConditionOperator.Equal, toExpSolutionUniqueName);                    qe.TopCount = 1;                    solutions = _serviceProxy.RetrieveMultiple(qe);                    if(solutions.Entities.Count >= 1)                    {                        var solution = solutions.Entities[0];                        solution["version"] = string.Format("8.2.{0}.{1}", DateTime.Now.Month, DateTime.Now.Day);                        _serviceProxy.Update(solution);                    }                    Console.WriteLine("开始发布所有自定义项" + DateTime.Now.ToLongTimeString());                    PublishAllXmlRequest pubReq = new PublishAllXmlRequest();                    _serviceProxy.Execute(pubReq);                    Console.WriteLine("开始导出" + DateTime.Now.ToLongTimeString());                    ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest();                    exportSolutionRequest.Managed = false;                    exportSolutionRequest.SolutionName = toExpSolutionUniqueName;                    exportSolutionRequest.TargetVersion = "8.2";//Dynamics 365导出时候可以选择目标环境用什么版本                    ExportSolutionResponse exportSolutionResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportSolutionRequest);                    byte[] exportXml = exportSolutionResponse.ExportSolutionFile;                    string filename = string.Format("{0}_{1}.zip", toExpSolutionUniqueName, solutions.Entities[0].GetAttributeValue<string>("version").Replace('.','_'));                    File.WriteAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).Replace("Documents", "Downloads") + "\\" + filename, exportXml);                    Console.WriteLine("开始导入" + DateTime.Now.ToLongTimeString());                    byte[] fileBytes = File.ReadAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).Replace("Documents", "Downloads") + "\\" + filename);                    ImportSolutionRequest impReq = new ImportSolutionRequest()                    {                        CustomizationFile = fileBytes                    };                    _serviceProxy.Execute(impReq);                    Console.WriteLine("开始发布所有自定义项" + DateTime.Now.ToLongTimeString());                    pubReq = new PublishAllXmlRequest();                    _serviceProxy.Execute(pubReq);                    Console.WriteLine("程序运行成功!");                    Console.ReadKey();                }            }            catch (FaultException ex)            {                Console.WriteLine("程序出现异常:ex.Message=" + ex.Message);                Console.ReadKey();            }        }

 

展示效果如下图:

 

      可能你会对PublishAllXmlRequest这个消息产生疑问,这个是发布哪个解决方案的所有自定义项,实验证明应该是发布所有解决方案的所有自定义项。在导出解决方案之前请执行下这个消息确保所有的自定义项都发布了,在导入解决方案之后也执行下这个消息导入后的解决方案生效。

      Dynamics 365的一个新增功能是导出解决方案时候可以指定解决方案的版本,可以选择 8.0, 8.1或者8.2,我这个导出程序使用了默认的 8.2.

值得说明的是高级查找并不能查询解决方案这个实体,所以我这里用的是QueryExpression这种查询方法,而不是时候用FetchXml来查询。当然两者是可以互相转换的。查看这个实体的元数据还是使用Dynamics 365提供的Metadata Browser这个解决方案吧。


©著作权归作者所有:来自51CTO博客作者luoyong0201的原创作品,如需转载,请注明出处,否则将追究法律责任

每一份赞赏源于懂得

赞赏

0人进行了赞赏支持

更多相关文章

  1. Puppet自动化集群管理进阶篇
  2. CentOS 7 安装以及配置桌面环境
  3. 使用开源软件打造类似Google的开发和生产环境
  4. Tomcat爆出严重漏洞,影响所有版本,附解决方案!
  5. Vue.js 基础入门系列(一)环境搭建
  6. maven 插件用于打不同环境的版本包
  7. win10系统与华为模拟器ensp完美结合解决方案
  8. Azure IoT解决方案
  9. Hadoop二次开发环境构建

随机推荐

  1. android selector 背景选择器
  2. Android系列教程之十:Intents and Intent
  3. Dagger2使用
  4. android监听键盘
  5. Android(安卓)5.X Activity过渡动画,以及
  6. Android的网络抓包工具Tcpdump
  7. 牛人博客收集
  8. 关于相对布局RelativeLayout的各种属性介
  9. Android(安卓)Activity 详解
  10. Android移动终端数据同步技术的一次了解