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

备用键(Alternate Key)是个不错功能,可以保证该键值唯一,会在数据库中创建一个唯一索引,但是如果现有数据该键值就是有重复的呢?就会创建失败。

如果不解决就会带来误会,以为创建成功了,其实没有,还是会产生重复键值,这个在编号时候常用备用键来确保唯一。

一个个去查看是否创建成功太麻烦,我这里搞了个程序可以实现查看所有的备用键的创建状态,不多说了,上代码。

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.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml; namespace RetrieveEntityAlternateKeys
{
class Program
{
static void Main(string[] args)
{
try
{
string inputKey;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
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"];
string needConfirm = ConfigurationManager.AppSettings["needConfirm"];
using (var orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
{
orgSvc.Timeout = new TimeSpan(, , );
WhoAmIRequest whoReq = new WhoAmIRequest();
var whoRsp = orgSvc.Execute(whoReq) as WhoAmIResponse;
var userEntity = orgSvc.Retrieve("systemuser", whoRsp.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname"));
Console.WriteLine(string.Format("欢迎【{0}】登陆到【{1}】", userEntity.GetAttributeValue<string>("fullname"), ConfigurationManager.AppSettings["orgUrl"]));
Console.WriteLine("本程序查看实体的备用键(Alternate Key)是否都成功创建了!");
if (needConfirm == "Y")
{
Console.WriteLine("当前处于需要确认才会继续的模式,若要继续请输入Y然后回车确认!");
inputKey = Console.ReadLine();
if (inputKey.ToUpper() == "Y")
{
RetrieveAlternateKeys(orgSvc);
}
else
{
Console.WriteLine("你选择了取消运行!");
}
}
else
{
RetrieveAlternateKeys(orgSvc);
}
}
Console.Write("程序运行完成,按任意键退出." + DateTime.Now.ToString());
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("程序运行出错:" + ex.Message + ex.StackTrace);
Console.ReadLine();
}
} private static void RetrieveAlternateKeys(OrganizationServiceProxy orgSvc)
{
const string functionName = "查看实体的备用键(Alternate Key)是否都成功创建了";
Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString()));
try
{
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Entity,
RetrieveAsIfPublished = true
};
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)orgSvc.Execute(request);
foreach (EntityMetadata currentEntity in response.EntityMetadata)
{
RetrieveEntityRequest eReq = new RetrieveEntityRequest()
{
LogicalName = currentEntity.LogicalName,
EntityFilters = EntityFilters.All,
RetrieveAsIfPublished = true
};
var eRep = (RetrieveEntityResponse)orgSvc.Execute(eReq);
foreach (var item in eRep.EntityMetadata.Keys)
{
EntityKeyIndexStatus status = (EntityKeyIndexStatus)item.EntityKeyIndexStatus;
if (status != EntityKeyIndexStatus.Active)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format(@"{0} - {1} - {2} - {3}", item.EntityLogicalName, item.LogicalName, item.DisplayName.UserLocalizedLabel.Label, item.EntityKeyIndexStatus.ToString()));
}
else
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(string.Format(@"{0} - {1} - {2} - {3}", item.EntityLogicalName, item.LogicalName, item.DisplayName.UserLocalizedLabel.Label, item.EntityKeyIndexStatus.ToString()));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace));
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString()));
Console.WriteLine("================================================");
} 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中的备用键状态的更多相关文章

  1. 介绍Dynamics 365 Customer Engagement中的备用键(alternate key)

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

  2. Dynamics 365中开发和注册插件介绍

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

  3. 自定义工作流活动报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。

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

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

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

  5. Dynamics 365工作流报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。

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

  6. Dynamics 365中的应用程序介绍

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

  7. Dynamics 365中配置和使用文件夹级别的跟踪(folder-level tracking)

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

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

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

  9. Dynamics 365中自定义工作流活动获取的上下文分析及注意事项

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

随机推荐

  1. async/await 的理解

    1.如果一个方法标记了 async 关键字,那么这个方法被调用时就是异步执行: 2.利用Task运行一个任务,这个任务里的函数也是异步执行: 3.如果一个任务前被标记await,那么等待这个任务执行完 ...

  2. 回顾4180天在腾讯使用C#的历程,开启新的征途

    今天是2018年8月8日,已经和腾讯解除劳动关系,我的公司正式开始运营,虽然还有很多事情需要理清,公司官网也没有做,接下来什么事情都需要自己去完成了,需要一步一个脚印去完善,开启一个新的征途,我将在博 ...

  3. 简单实用而不追求时髦的 Vim 配置

    前言 由于 Vim 的广泛流行,在网络上关于 Vim 的自定义配置汗牛充栋.既有高手 Tim Pope 的极简配置 tpope/vim-sensible(这个配置一个插件都没有),也有 spf13/s ...

  4. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  5. [Swift]LeetCode822. 翻转卡片游戏 | Card Flipping Game

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  6. spring-cloud-sleuth+zipkin源码探究

    1. spring-cloud-sleuth+zipkin源码探究 1.1. 前言   粗略看了下spring cloud sleuth core源码,发现内容真的有点多,它支持了很多类型的链路追踪, ...

  7. PHP算法之二分查找

    二分查找: 数组必须有序,且不重复. 一般实际工作中,很少有这样的数组,所以应用的很少,但是思想很好. 1 // 二分查找 2 $array = [10,14,23,33,45,56,65,77,89 ...

  8. Python面试真题第三节

    51.正则匹配,匹配日期2018-03-20 url='https://sycm.taobao.com/bda/tradinganaly/overview/get_summary.json?dateR ...

  9. PHP常用设计模式讲解

    开发中适当的使用设计模式,可以让项目有更易扩展,易维护.低耦合,代码简洁等 单例模式 <?php /** * 单例模式:使类在全局范围内只允许创建一个对象,常用于数据库连接等 */ class ...

  10. the python challenge闯关记录(0-8)

    0 第零关 2**38 = 274877906944 下一关的url:http://www.pythonchallenge.com/pc/def/274877906944.html 1 第一关 移位计 ...