您的位置:首页 > 其它

MS CRM 2011的自定义和开发(10)——CRM web服务介绍(第二部分)——IOrganizationService(一)

2012-01-28 23:18 621 查看
上一篇文章,介绍了IDiscoveryServiceweb服务,本篇介绍CRM平台中,使用频度最高的web服务,IOrganizationServiceweb服务。

下图是IOrnigazationService服务相关的几个类的类图





上图中,包含了以下几部分信息:


IOrganizationService接口;

IOrganizationService接口的实现类OrganizationServiceProxy;

IOrganizationService接口中的CRUD方法需要使用到的类Entity;

IOrganizationService接口的Execute方法需要使用到的OrganizationRequest请求以及OrganizationResponse响应;


下面进行详细介绍:

首先,说一下OrganizationServiceProxy类的实例化。OrganizationServiceProxy类的构造函数有5个重载,分别是:

签名

说明
OrganizationServiceProxy(IServiceConfiguration[IOrganizationService],SecurityTokenResponse)

使用服务配置信息以及安全令牌响应创建OrganizationServiceProxy实例

OrganizationServiceProxy(IServiceConfiguration[IOrganizationService],ClientCredentials)

使用服务配置以及用户凭据创建OrganizationServiceProxy实例

OrganizationServiceProxy(IServiceManagement[IOrganizationService],SecurityTokenResponse)

使用服务管理以及安全令牌响应创建OrganizationServiceProxy实例

OrganizationServiceProxy(IServiceManagement[IOrganizationService],ClientCredentials)

使用服务管理以及用户凭据创建OrganizationServiceProxy实例

OrganizationServiceProxy(Uri,Uri,ClientCredentials,ClientCredentials)

根据组织服务Uri,客户凭据等信息创建OrganizationServiceProxy实例

上面表格中提到的IServiceManagement实例的创建方法,通过Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory类的静态方法IServiceManagement<TService>CreateManagement<TService>(UriserviceUri)获取,对于IServiceConfiguration实例,可以通过Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory类的静态方法IServiceConfiguration<TService>CreateConfiguration<TService>(UriserviceUri)获取,两个静态方法中的输入参数userviceUri,是目标组织的OrganizationServiceweb服务的Uri。

上面表格中的最后一个构造函数,是将组织服务Uri,homeRealmUri,登陆用户的Credentials以及设备Credentials作为参数传递给构造函数。样例代码如下:



UriorgServiceUri=newUri("http://<CrmServer>:<Port>/<OrgName>/XrmServices/2011/Organization.svc");
ClientCredentialsclientCredentials=newClientCredentials();
clientCredentials.Windows.ClientCredential=newNetworkCredential("<UserName>","<Password>","<DomainName>");
OrganizationServiceProxyproxy=newOrganizationServiceProxy(orgServiceUri,null,clientCredentials,null);


在没有第二个ADFS服务实例的时候,homeRealmUri参数值为null,在On-Premise部署模式下,第四个参数deviceCredentials的值为null,否则需要使用SDK中的DeviceIdManager工具去获取设备凭据。


IOrnigazationService接口公布了Create、Update、Delete、Retrieve、RetrieveMultiple、Associate、Disassociate以及Execute方法。其中

Create方法用于完成实体实例的创建工作。其完整签名如下:

publicGuidCreate(Entityentity)

从签名可以看出,该方法接收一个类型为Entity的参数,该方法的返回值是Guid。从数据库角度讲,就是在实体所对应的数据表中插入一条记录,数据表的主键列的数据类型是UniqueIdentifier,这个字段的值,对应了Entity类中的Id属性,也是Create方法的返回值。

下面是Create方法的样例代码,



//创建Entity实例,在Entity类的构造函数中,使用实体逻辑名称作为输入参数。
Entityaccount=newEntity("account");

//设定必填项,对于客户实体而言,必填项只有”name”属性。

//各位同学在自己编写代码过程中,需要根据实际情况,确定必填字段。

account["name"]="FourthCoffee";

//_service的类型是Organization
_accountId=_service.Create(account);




Update方法用于对特定实体实例的某些字段进行更新,其签名如下:


publicvoidUpdate(Entityentity)


从签名可以看出,其输入参数和Create方法相同。Update方法没有返回参数,样例代码如下:



Entityaccount=newEntity("account");

//_accountId来自于Create方法样例代码

account.Id=_accountId;

//设定address1_postalcode字段值,该字段的数据类型是单行文本

account["address1_postalcode"]="98052";

//设定address2_postalcode字段值为空.
account["address2_postalcode"]=null;

//设定revenue字段值,该字段的数据类型三Money类型.
account["revenue"]=newMoney(5000000);

//设定creditonhold字段值,该字段数据类型是“两个选项”,即boolean型.
account["creditonhold"]=false;

//更新客户实例.
_service.Update(account);



Delete方法用于删除特定实体实例,其签名如下:


publicvoidDelete(stringentityName,Guidid)


从签名可以看出,接收参数分别指定了待删除实例的实体逻辑名称,以及待删除实例的Guid主键值。样例代码如下:



//_accountId来自于Create方法样例代码

2_service.Delete("account",_accountId);




以上,介绍了OrganizationProxy类的构造函数,IOrganizationService接口公布的Create、Update、Delete方法。在后续文章中将介绍其他方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航