本人微信公众号:微软动态CRM专家罗勇 ,回复281或者20181116可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
有时候我想将系统中的实体信息导出来,若是多语言,一个实体会有多个显示名称,有时候对应起来不方便,我便写了个程序导出来,实例是导出简体中文和英文的显示名称,架构名称,XrmtoolsBox也可以导出,不过导出时候是实体的逻辑名称,架构名称有时候更有用,架构名称转成逻辑名称方便,全小写就行了。不多说了,上代码,主要参考官方的 RetrieveAllEntitiesRequest Class 官方实例更加详细,导出的是XML文件,我这里转成Excel,大家用这个更多点。
1 using Microsoft.Crm.Sdk.Messages; 2 using Microsoft.Xrm.Sdk; 3 using Microsoft.Xrm.Sdk.Client; 4 using Microsoft.Xrm.Sdk.Messages; 5 using Microsoft.Xrm.Sdk.Metadata; 6 using Microsoft.Xrm.Sdk.Query; 7 using System; 8 using System.Configuration; 9 using System.Linq; 10 using System.ServiceModel.Description; 11 using Excel = Microsoft.Office.Interop.Excel; 12 13 namespace ExportEntityMetadata 14 { 15 class Program 16 { 17 static void Main(string[] args) 18 { 19 IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"])); 20 AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials(); 21 orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"]; 22 orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"]; 23 using (OrganizationServiceProxy orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials)) 24 { 25 WhoAmIRequest whoReq = new WhoAmIRequest(); 26 WhoAmIResponse whoRep = orgSvc.Execute(whoReq) as WhoAmIResponse; 27 var userEntity = orgSvc.Retrieve("systemuser", whoRep.UserId, new ColumnSet("fullname")); 28 Console.WriteLine(string.Format("登录组织{0}成功,欢迎{1},继续操作请输入y!", ConfigurationManager.AppSettings["orgUrl"], userEntity.GetAttributeValue<string>("fullname"))); 29 var input = Console.ReadLine().ToString().ToUpper(); 30 if (input == "Y") 31 { 32 Console.WriteLine(string.Format("程序开始处理 - {0}", DateTime.Now.ToString())); 33 RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest() 34 { 35 EntityFilters = EntityFilters.Entity, 36 RetrieveAsIfPublished = true 37 }; 38 RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)orgSvc.Execute(request); 39 var excelApp = new Excel.Application(); 40 excelApp.Visible = false; 41 Excel.Workbook metadataWorkbook = excelApp.Workbooks.Add(); 42 Excel.Worksheet rolesWorksheet = (Excel.Worksheet)excelApp.ActiveSheet; 43 rolesWorksheet.Name = "实体信息"; 44 int row = 1; 45 rolesWorksheet.Cells[1, 1] = "实体架构名称"; 46 rolesWorksheet.Cells[1, 2] = "所有者类型"; 47 rolesWorksheet.Cells[1, 3] = "是否活动实体"; 48 rolesWorksheet.Cells[1, 4] = "实体显示名称(中文)"; 49 rolesWorksheet.Cells[1, 5] = "实体显示名称(英文)"; 50 rolesWorksheet.Cells[1, 6] = "实体说明"; 51 rolesWorksheet.Rows[1].Font.Bold = true;//字体加粗 52 row++; 53 54 foreach (EntityMetadata currentEntity in response.EntityMetadata) 55 { 56 rolesWorksheet.Cells[row, 1] = currentEntity.SchemaName; 57 rolesWorksheet.Cells[row, 2] = currentEntity.OwnershipType.Value.ToString(); 58 rolesWorksheet.Cells[row, 3] = currentEntity.IsActivity.Value.ToString(); 59 rolesWorksheet.Cells[row, 4] = currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == 2052).Count() >= 1 ? currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == 2052).FirstOrDefault().Label : string.Empty; 60 rolesWorksheet.Cells[row, 5] = currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == 1033).Count() >= 1 ? currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == 1033).FirstOrDefault().Label : string.Empty; 61 rolesWorksheet.Cells[row, 6] = currentEntity.Description.LocalizedLabels.Where(a => a.LanguageCode == 2052).Count() >= 1 ? currentEntity.Description.LocalizedLabels.Where(a => a.LanguageCode == 2052).FirstOrDefault().Label : string.Empty; ; 62 row++; 63 Console.WriteLine(string.Format("第{0}行处理完毕 - {1}", row - 1, DateTime.Now.ToString())); 64 } 65 rolesWorksheet.Columns[1].AutoFit();//自动列宽 66 rolesWorksheet.Columns[2].AutoFit();//自动列宽 67 rolesWorksheet.Columns[3].AutoFit();//自动列宽 68 rolesWorksheet.Columns[4].AutoFit();//自动列宽 69 rolesWorksheet.Columns[5].AutoFit();//自动列宽 70 rolesWorksheet.Columns[6].AutoFit();//自动列宽 71 metadataWorkbook.SaveAs(Filename: @"D:\CRMMetadata.xlsx", FileFormat: Excel.XlFileFormat.xlWorkbookDefault); 72 metadataWorkbook.Close(); 73 excelApp.Quit(); 74 } 75 } 76 Console.Write("程序执行完毕!"); 77 Console.ReadKey(); 78 } 79 80 private static TProxy GetProxy<TService, TProxy>( 81 IServiceManagement<TService> serviceManagement, 82 AuthenticationCredentials authCredentials) 83 where TService : class 84 where TProxy : ServiceProxy<TService> 85 { 86 Type classType = typeof(TProxy); 87 88 if (serviceManagement.AuthenticationType != 89 AuthenticationProviderType.ActiveDirectory) 90 { 91 AuthenticationCredentials tokenCredentials = 92 serviceManagement.Authenticate(authCredentials); 93 return (TProxy)classType 94 .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) }) 95 .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse }); 96 } 97 return (TProxy)classType 98 .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) }) 99 .Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });100 }101 }102 }