今天折磨了一小天总算把问题解决了,首先描述一下问题。
1,写CRM2011 的webService接口, 在查询数据的时候需要进行权限控制。
首先想到的办法是用CRM中标准的 IOrganizationService 对象,但是常规的方法中必须要知道登录的用户名,密码才能生成对象。
但实际情况是访问webService是域认证的方式不能取得密码,所以不能通过用户名密码的方式生成该对象。
但是SDK中其实还提供了一种方式只不过是我们不常用而已:通过用户凭证生成 IOrganizationService 对象。
这样的话问题就解决了。因为在域认证的情况下我们可以获得他的域凭证。
闲话少说上代码:
[WebMethod]
public int getService()
{
Uri orgServiceUri = new Uri("http://localhost:5555/orgName/XRMServices/2011/Organization.svc");
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
OrganizationServiceProxy crmServiceProxy = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);
IOrganizationService crmService = (IOrganizationService)crmServiceProxy;
//crmService.Delete("contact", new Guid("B53FA2A9-76CB-E411-8BE6-B82A72D5A332"));
String fetchXml = @"<fetch mapping=""logical"" count=""50"" version=""1.0"">
<entity name=""contact"">
<attribute name=""address1_telephone1"" />
<attribute name=""contactid"" />
<attribute name=""firstname"" />
<attribute name=""lastname"" />
</entity>
</fetch>";
// Build fetch request and obtain results.
RetrieveMultipleRequest efr = new RetrieveMultipleRequest()
{
Query = new FetchExpression(fetchXml)
};
EntityCollection entityResults = ((RetrieveMultipleResponse)crmService.Execute(efr)).EntityCollection;
int count = 0;
count = entityResults.Entities.Count;
return count;
}
换两个有不同角色的用户调用该方法,返回的值如下: