[译]Dynamics CRM 4.0 Web服务SDK示例
By robot-v1.0
本文链接 https://www.kyfws.com/applications/dynamics-crm-4-0-web-service-sdk-example-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2747 个词 阅读量 0[译]Dynamics CRM 4.0 Web服务SDK示例
原文地址:https://www.codeproject.com/Articles/43291/Dynamics-CRM-4-0-Web-Service-SDK-Example
原文作者:AymanAminIbrahim
译文由本站 robot-v1.0 翻译
前言
Using Dynamics CRM Web Service to Query & Create User Calendar
使用Dynamics CRM Web服务查询和创建用户日历
介绍(Introduction)
Microsoft Dynamics CRM 4.0是一个完整的业务应用程序平台,可以进行自定义和扩展,以适应复杂的和琐碎的业务需求.扩展此平台可用功能的一种方法是使用SDK并编写代码以调用CRM标准Web服务,以执行查询和更新CRM实体等上的数据操作.(Microsoft Dynamics CRM 4.0 is a complete Business Application Platform that is open for customization as well as extension to suit sophisticated as well as trivial business needs. One way to extend the functionality available for this platform is to use the SDK and write code to call the CRM standard Web services to perform queries and update data operations on CRM Entities, etc.)
这个由3部分组成的系列文章介绍了一种简单的方法,该方法使用Dynamics CRM 4.0 Web服务为恰好具有特定角色(在本例中为销售代表)的给定用户连接,查询,创建实体记录,然后创建用户工作天数/小时数与他/她所获得的用户角色相关的他/她的日历.(This 3 part series explains one easy way of using Dynamics CRM 4.0 web services to connect, query, create entity records for a given user who happens to have a specific role (Sales Rep. in our case) and then create User Work Days / Hours Calendar for him /her that is relevant to the User Role he /she is given.)
背景(Background)
在Microsoft Dynamics CRM 4.0中,没有开箱即用的功能来进行复制/克隆,然后将用户工作天/小时日历从一个用户或模板粘贴到另一个用户.因此,本系列文章中的代码可以帮助在创建用户时面临此尴尬任务的开发人员/实施人员在创建用户或向现有用户添加角色时需要输入批量和模板/标准属性.(In Microsoft Dynamics CRM 4.0 it is not provided out of the box to have a copy / clone then paste User Work Days/hours Calendar from one user or a template to another user. Hence the code in the article series can help a developer / implementor who is faced with this awkward task when users are created En mass and template / standard properties are required to be entered when a User is created or a role is added to an existing user.)
使用代码(Using the Code)
以下几点逐步说明了代码策略,将扩展到本文的3部分系列:(The following points state the code strategy step by step which will be stretched to the 3 part series of this article:)
- 与CRM服务器实例建立连接.(Establish a connection with a CRM Server instance.)
- 获取CRM发现Web服务的实例(通过Active Directory进行身份验证).(Obtain an instance of the CRM discovery Web Service (authenticate via Active Directory).)
- 找到目标组织,然后获取安全令牌并读取CRM Web服务的URL.(Locate the target organization, then obtain a security token and read the URL for the CRM Web Service.)
- 使用CRM Web服务执行查询以获取分配给给定用户的角色.(Use CRM Web Service to Execute a Query to Obtain roles assigned to a given user.)
- 测试该用户是否具有销售代表的角色.(Test if that User has a Role of Sales Rep. If)
true
…(…) - 使用CRM Web服务创建与用户相关的工作日和日历,从模板标准用户日历(或可能从内部数据结构)中读取. ((Use CRM Web Service to create a user related work Days and Hours Calendar that is read from a template standard user Calendar (or could be from an internal Data Structure). () 第二部分(Part II) )()) ()注意:(Note:)稍后,CRM工作流自定义活动应执行实现为通用Windows Command Shall实用工具的系统作业,以调用此控制台应用程序并传递用户登录名参数,以自动创建用户工作天数/小时数,当用户角色触发时为用户提供了销售代表的数量. (( Later on, a CRM Workflow custom activity shall execute a system job implemented as a generic Windows Command Shall utility to call this console application and pass a user login * parameter for automatic creation of a user working Days/hours Calendar triggered when a User role of Sales Rep. is given for the user. () 第三部分(Part III) ).().*)
在开始对任何基于Dynamics CRM 4.0 Web服务的应用程序进行编码之前,必须在项目解决方案资源管理器中的Web引用树分支下设置旨在使用Web服务的Web引用.(Before starting to code any Dynamics CRM 4.0 web services based application, a web reference intended to use web services must be set in the Project solution explorer under the web references tree branch.)
在这个例子中,已经设置了2个Web参考.第一个是对由CRM 4.0平台发布并命名为"发现" Web服务的引用(In this example, 2 web references have been set; the first is a reference to the Discovery web services published by the CRM 4.0 platform and named) CrmSdk.Discovery
第二个是对CRM 4.0 CRM Web服务的Web引用,其名称为(and the second is a web reference to the CRM 4.0 CRM web Service and named) CrmSdk
.指向两个Web引用的URL的格式都与CRM 4.0 Server组件的基于本地Active Directory的部署相匹配.(. The URLs that points to both web references are formatted to match an on-premise Active Directory based deployment of the CRM 4.0 Server components.)
然后,两个引用都包含在(Both references are then included in the) using
程序集声明位于源代码的顶部,并在此示例中为控制台应用程序项目提供了命名空间.(assembly declaration at the top of the source code prefixed by the name space given to the Console Application project of this example.)
首先,我们首先需要传递2个命令行参数来设置CRM服务器用户登录名和密码..(As we start, we first need to pass 2 command line parameters to set the CRM Server user login and a password ..)
if (args.Length != 2)
{
Console.WriteLine( ...);
Return 1;
}
然后,以下代码用于创建/配置Crm Discovery Service Web服务代理的实例.(Then the following code serves to create / configure an Instance of the Crm Discovery Service Web Service Proxy.)
注意:(Note:) Url
为简单起见,该属性作为硬编码字符串提供,但应作为参数传递或从XML Helper文件读取.(property is provided as a hardcode string for simplicity but should be passed as a parameter or read from an XML Helper file.)
CrmDiscoveryService discoveryservice = new CrmDiscoveryService();
discoveryservice.UseDefaultCredentials = true;
discoveryservice.Url = ...;
最后一个(Finally an) Execute
在Discovery服务对象实例上执行的方法调用ID,以将组织集作为(method call id performed on the Discovery service object instance to retrieve the set of organizations as a) RetrieveOrganizationsResponse
然后解析CRM类型和(CRM type which is then parsed and a) Url
获取CRM服务的" ID"以初始化CRM Web服务的对象实例.(to the CRM Service is obtained to initialize an object instance of the CRM Web Service.)
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
...
RetrieveOrganizationsResponse orgResponse =
(RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
CrmService service = new CrmService();
...
建立连接并获得CRM Web服务的代理后,以下代码将创建标准查询表达式,然后使用3个表(内部之间使用(Once a connection is established and a proxy to the CRM web service is obtained, the following code is a standard query expression creation then retrieval of the User roles using 3 tables with inner join between) Role
和(and) UserRoles
然后之间(then between) UserRoles
和(and) User
表在哪里(table Where) UserRoles
是相交表.请参考所附的源代码.(is the intersect table. Please refer to the source code attached.)
qe = new QueryExpression();
qe.EntityName = "role";
qe.ColumnSet = new AllColumns();
...
LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "role";
le.LinkFromAttributeName = "roleid";
le.LinkToEntityName = "systemuserroles";
le.LinkToAttributeName = "roleid";
// Set up the join between the intersect table "systemuserroles"
// and the "systemuser" entity.
LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "systemuserroles";
le2.LinkFromAttributeName = "systemuserid";
le2.LinkToEntityName = "systemuser";
le2.LinkToAttributeName = "systemuserid";
// The condition is WHERE user ID = myUserId.
ce = new ConditionExpression();
ce.AttributeName = "systemuserid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { myUserId };
le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
le.LinkEntities = new LinkEntity[] { le2 };
qe.LinkEntities = new LinkEntity[] { le };
// Execute the query.
bec = service.RetrieveMultiple(qe);
...
兴趣点(Points of Interest)
当捕获用于检索实体记录并查询CRM后端数据库的关键代码路径的异常时,重要的是更新(When catching an exception for the critical code path that retrieves entity records and querying the CRM backend database, it is important to update the)**web.config(web.config)**CRM Web应用程序以更改密钥的值(of the CRM web application to change the value of the Key) DevErrors
设置为(to be set to) on
不(not) off
.这将指示CRM运行时将更有意义的错误消息发送给(. This will instruct CRM runtime to throw more meaningful error messages to the) try catch
调用CRM Web服务时代码中的异常块.(exception blocks in your code when calling the CRM Web Service.)
历史(History)
- 本文部分的第一版(First version of this article part)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Vista .NET3.0 .NET3.5 Windows Win2003 Dev 新闻 翻译