爲Dynamics 365寫一個簡單程序實現解決方案一鍵遷移

關注本人微信和易信公衆號: 微軟動態CRM專家羅勇 ,回覆258或者20170627可方便獲取本文,同時能夠在第一間獲得我發佈的最新的博文信息,follow me!個人網站是 www.luoyong.me 。微信

        在咱們作項目的過程當中,通常會涉及到多個Dynamics 365環境,通常包括一個開發環境、一個SIT環境,一個UAT環境和一個生產環境,常常涉及到解決方案從開發環境遷移到SIT環境,從開發環境遷移到UAT環境,從開發環境遷移到UAT環境等等。通常手工操做是先更改解決方案版本,保存後發佈解決方案,再導出解決方案,再導入解決方案到目標環境。一個解決方案還好,解決方案多了麻煩,容易手誤或者漏操做,能夠寫個程序來作這些繁雜的事情嗎?Follow me。ide

       直接上代碼,代碼中有註釋說明,注意我這裏是導入到同一個環境,你使用個人代碼的時候要改動到其餘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();
            }
        }

複製代碼

 

展現效果以下圖:orm

 

      可能你會對PublishAllXmlRequest這個消息產生疑問,這個是發佈哪一個解決方案的全部自定義項,實驗證實應該是發佈全部解決方案的全部自定義項。在導出解決方案以前請執行下這個消息確保全部的自定義項都發布了,在導入解決方案以後也執行下這個消息導入後的解決方案生效。blog

      Dynamics 365的一個新增功能是導出解決方案時候能夠指定解決方案的版本,能夠選擇 8.0, 8.1或者8.2,我這個導出程序使用了默認的 8.2.ip

值得說明的是高級查找並不能查詢解決方案這個實體,因此我這裏用的是QueryExpression這種查詢方法,而不是時候用FetchXml來查詢。固然二者是能夠互相轉換的。查看這個實體的元數據仍是使用Dynamics 365提供的Metadata Browser這個解決方案吧。ci

相關文章
相關標籤/搜索