自定义控制台程序导出Dynamics365实体信息到Excel中。

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

自定义控制台程序导出Dynamics365实体信息到Excel中。

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

有时候我想将系统中的实体信息导出来,若是多语言,一个实体会有多个显示名称,有时候对应起来不方便,我便写了个程序导出来,实例是导出简体中文和英文的显示名称,架构名称,XrmtoolsBox也可以导出,不过导出时候是实体的逻辑名称,架构名称有时候更有用,架构名称转成逻辑名称方便,全小写就行了。

不多说了,上代码,主要参考官方的 RetrieveAllEntitiesRequest Class 官方实例更加详细,导出的是XML文件,我这里转成Excel,大家用这个更多点。

1using Microsoft.Crm.Sdk.Messages;
2using Microsoft.Xrm.Sdk;
3using Microsoft.Xrm.Sdk.Client;
4using Microsoft.Xrm.Sdk.Messages;
5using Microsoft.Xrm.Sdk.Metadata;
6using Microsoft.Xrm.Sdk.Query;
7using System;
8using System.Configuration;
9using System.Linq;
10using System.ServiceModel.Description;
11using Excel = Microsoft.Office.Interop.Excel;
12
13namespace ExportEntityMetadata
14{
15class Program
16 {
17static void Main(string[] args)
18 {
19IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganization Service>(new Uri(ConfigurationManager.AppSettings["orgUrl"]));
20 AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials();
21
erName = ConfigurationManager.AppSettings["userName"];
22
erName.Password = ConfigurationManager.AppSettings["passWord"];
23using(OrganizationServiceProxy orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
24 {
25WhoAmIRequest whoReq = new WhoAmIRequest();
26WhoAmIResponse whoRep = orgSvc.Execute(whoReq) as WhoAmIResponse;
27var userEntity = orgSvc.Retrieve("systemuser", erId, new ColumnSet("fullname"));
28 Console.WriteLine(string.Format("登录组织{0}成功,欢迎{1},继续操作请输入y!", ConfigurationManager.AppSettings["orgUrl"],
userEntity.GetAttributeValue<string>("fullname")));
29var input = Console.ReadLine().ToString().ToUpper();
30if (input == "Y")
31 {
32 Console.WriteLine(string.Format("程序开始处理 - {0}", DateTime.Now.T oString()));
33 RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
34 {
35 EntityFilters = EntityFilters.Entity,
36 RetrieveAsIfPublished = true
37 };
38 RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)orgSvc.Execute(request);
39var excelApp = new Excel.Application();
40 excelApp.Visible = false;
41Excel.Workbook metadataWorkbook = excelApp.Workbooks.Add();
42Excel.Worksheet rolesWorksheet = (Excel.Worksheet)excelApp.ActiveSheet;
43 = "实体信息";
44int 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
54foreach (EntityMetadata currentEntity in response.EntityMetadata)
55 {
56rolesWorksheet.Cells[row, 1] = currentEntity.SchemaName;
57rolesWorksheet.Cells[row, 2] = currentEntity.OwnershipType.Value.T oString();
58rolesWorksheet.Cells[row, 3] = currentEntity.IsActivity.Value.ToString();
59rolesWorksheet.Cells[row, 4] = currentEntity.DisplayName.LocalizedLabels.Where(a => nguageCode == 2052).Count() >= 1? currentEntity.DisplayName.LocalizedLabels.Where(a => nguageCode == 2052).FirstOrDefault().Label : string.Empty;
60rolesWorksheet.Cells[row, 5] = currentEntity.DisplayName.LocalizedLabels.Where(a => nguageCode == 1033).Count() >= 1? currentEntity.DisplayName.LocalizedLabels.Where(a => nguageCode == 1033).FirstOrDefault().Label : string.Empty;
61rolesWorksheet.Cells[row, 6] = currentEntity.Description.LocalizedLabels.Where(a => nguageCode == 2052).Count() >= 1? currentEntity.Description.LocalizedLabels.Where(a =>
nguageCode == 2052).FirstOrDefault().Label : string.Empty; ;
62 row++;
63 Console.WriteLine(string.Format("第{0}行处理完毕 - {1}", row - 1, DateTime.Now.T oString()));
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();//自动列宽
71metadataWorkbook.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
80private static TProxy GetProxy<TService, TProxy>(
81 IServiceManagement<TService> serviceManagement,
82AuthenticationCredentials authCredentials)
83where TService : class
84where TProxy : ServiceProxy<TService>
85 {
86 Type classType = typeof(TProxy);
87
88if (serviceManagement.AuthenticationType !=
89 AuthenticationProviderType.ActiveDirectory)
90 {
91 AuthenticationCredentials tokenCredentials =
92
serviceManagement.Authenticate(authCredentials);
93return (TProxy)classType
94.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityT okenResponse) })
95 .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityT okenResponse });
96 }
97return (TProxy)classType
98.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
99 .Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
100 }
101 }
102 }。

相关文档
最新文档