CRM WebService 域验证 获取service

18 篇文章 0 订阅
16 篇文章 0 订阅

今天折磨了一小天总算把问题解决了,首先描述一下问题。
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;

        }

换两个有不同角色的用户调用该方法,返回的值如下:
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值