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

有时候我想将系统中的实体信息导出来,若是多语言,一个实体会有多个显示名称,有时候对应起来不方便,我便写了个程序导出来,实例是导出简体中文和英文的显示名称,架构名称,XrmtoolsBox也可以导出,不过导出时候是实体的逻辑名称,架构名称有时候更有用,架构名称转成逻辑名称方便,全小写就行了。不多说了,上代码,主要参考官方的 RetrieveAllEntitiesRequest Class 官方实例更加详细,导出的是XML文件,我这里转成Excel,大家用这个更多点。

 using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Description;
using Excel = Microsoft.Office.Interop.Excel; namespace ExportEntityMetadata
{
class Program
{
static void Main(string[] args)
{
IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"]));
AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials();
orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"];
orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"];
using (OrganizationServiceProxy orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
{
WhoAmIRequest whoReq = new WhoAmIRequest();
WhoAmIResponse whoRep = orgSvc.Execute(whoReq) as WhoAmIResponse;
var userEntity = orgSvc.Retrieve("systemuser", whoRep.UserId, new ColumnSet("fullname"));
Console.WriteLine(string.Format("登录组织{0}成功,欢迎{1},继续操作请输入y!", ConfigurationManager.AppSettings["orgUrl"], userEntity.GetAttributeValue<string>("fullname")));
var input = Console.ReadLine().ToString().ToUpper();
if (input == "Y")
{
Console.WriteLine(string.Format("程序开始处理 - {0}", DateTime.Now.ToString()));
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Entity,
RetrieveAsIfPublished = true
};
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)orgSvc.Execute(request);
var excelApp = new Excel.Application();
excelApp.Visible = false;
Excel.Workbook metadataWorkbook = excelApp.Workbooks.Add();
Excel.Worksheet rolesWorksheet = (Excel.Worksheet)excelApp.ActiveSheet;
rolesWorksheet.Name = "实体信息";
int row = ;
rolesWorksheet.Cells[, ] = "实体架构名称";
rolesWorksheet.Cells[, ] = "所有者类型";
rolesWorksheet.Cells[, ] = "是否活动实体";
rolesWorksheet.Cells[, ] = "实体显示名称(中文)";
rolesWorksheet.Cells[, ] = "实体显示名称(英文)";
rolesWorksheet.Cells[, ] = "实体说明";
rolesWorksheet.Rows[].Font.Bold = true;//字体加粗
row++; foreach (EntityMetadata currentEntity in response.EntityMetadata)
{
rolesWorksheet.Cells[row, ] = currentEntity.SchemaName;
rolesWorksheet.Cells[row, ] = currentEntity.OwnershipType.Value.ToString();
rolesWorksheet.Cells[row, ] = currentEntity.IsActivity.Value.ToString();
rolesWorksheet.Cells[row, ] = currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == ).Count() >= ? currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == ).FirstOrDefault().Label : string.Empty;
rolesWorksheet.Cells[row, ] = currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == ).Count() >= ? currentEntity.DisplayName.LocalizedLabels.Where(a => a.LanguageCode == ).FirstOrDefault().Label : string.Empty;
rolesWorksheet.Cells[row, ] = currentEntity.Description.LocalizedLabels.Where(a => a.LanguageCode == ).Count() >= ? currentEntity.Description.LocalizedLabels.Where(a => a.LanguageCode == ).FirstOrDefault().Label : string.Empty; ;
row++;
Console.WriteLine(string.Format("第{0}行处理完毕 - {1}", row - , DateTime.Now.ToString()));
}
rolesWorksheet.Columns[].AutoFit();//自动列宽
rolesWorksheet.Columns[].AutoFit();//自动列宽
rolesWorksheet.Columns[].AutoFit();//自动列宽
rolesWorksheet.Columns[].AutoFit();//自动列宽
rolesWorksheet.Columns[].AutoFit();//自动列宽
rolesWorksheet.Columns[].AutoFit();//自动列宽
metadataWorkbook.SaveAs(Filename: @"D:\CRMMetadata.xlsx", FileFormat: Excel.XlFileFormat.xlWorkbookDefault);
metadataWorkbook.Close();
excelApp.Quit();
}
}
Console.Write("程序执行完毕!");
Console.ReadKey();
} private static TProxy GetProxy<TService, TProxy>(
IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
where TService : class
where TProxy : ServiceProxy<TService>
{
Type classType = typeof(TProxy); if (serviceManagement.AuthenticationType !=
AuthenticationProviderType.ActiveDirectory)
{
AuthenticationCredentials tokenCredentials =
serviceManagement.Authenticate(authCredentials);
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
.Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
}
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
.Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
}
}
}

自定义控制台程序导出Dynamics 365实体信息到Excel中。的更多相关文章

  1. 自定义控制台程序导出角色对实体的权限为Excel文件

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

  2. 微信公众号开发之如何一键导出微信所有用户信息到Excel

    微信开发交流群:148540125 系列文章参考地址 极速开发微信公众号欢迎留言.转发.打赏 项目源码参考地址 点我点我--欢迎Start 极速开发微信公众号系列文章之如何一键导出微信所有用户信息到E ...

  3. 无依赖简单易用的Dynamics 365实体记录数计数器并能计算出FetchXml返回的记录数

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

  4. 嵌入Canvas App到Dynamics 365 Customer Engagement(Model-Driven App)中,创造更多可能!

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  5. 控制台程序读取Excel设置角色权限

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

  6. 将Dynamics 365中的用户及其角色、角色导出到Excel中

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

  7. Dynamics 365 POA表记录的查询

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

  8. Dynamics 365中的非交互式账号(Non-interactive User)介绍

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

  9. Dynamics 365中的事件框架与事件执行管道(Event execution pipeline)

    本文介绍了Microsoft Dynamics 365(以下简称D365)中的两个概念,事件框架(Event Framework)与事件执行管道(Event execution pipeline). ...

随机推荐

  1. Portainer介绍

    Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服 ...

  2. MySQL 数据库字段类型使用说明

    简介 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型. 数值类型 下列用于描述的代码字母中: M表示最大的显示尺寸.最大的合法的显示尺寸是 255 .(注 ...

  3. 如何将多个数据的- 转为:来匹配josn格式

    var str = `北京 2 河北 3 河北-邯郸 301 河北-保定 302 河北-邢台 303 山东 4 山东-青岛 401 山东-烟台 402`; var arrStr = str.split ...

  4. [Swift]LeetCode57. 插入区间 | Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  6. [Swift]LeetCode849. 到最近的人的最大距离 | Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  7. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  8. [Swift]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

    Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...

  9. awk小例子_2_数值统计脚本

    通信公司工作,经常处理各种协议接口,在统计协议接口字段内容时,需要统计字段填写的内容是否正确,和占比是多少.要是单次统计,估计会把人累死,写个脚本统计,轻松便捷. 举例:接口内容 这是一条话单,这样的 ...

  10. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...