如何在Microsoft Dynamics CRM 2011中查询机会

时间:2011-08-15 03:12:47

标签: dynamics-crm dynamics-crm-2011

我正在尝试查询Microsoft Dynamincs CRM 2011中的商机信息。

知道为什么我一直收到 401 Unauthorized 错误?

如果我在浏览器中使用该网址似乎有效。

Uri organizationUri = new Uri("/XRMServices/2011/OrganizationData.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
// Get the IOrganizationService
IOrganizationService orgService = (IOrganizationService)orgProxy;
//Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
//a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
OrganizationServiceContext orgServiceContext = new OrganizationServiceContext(orgService);

// Get name,number and ownerid for all the account records
var queryAccount1 = from r in orgServiceContext.CreateQuery("opportunity")
                    select new
                    {
                        CustomerID = r["customerid"],
                    };

foreach (var account in queryAccount1)
{
    txtCustomerID.Text = account.CustomerID.ToString();
}

1 个答案:

答案 0 :(得分:1)

您是在Intranet还是IFD上访问CRM?我认为问题在于您设置凭据的方式。

如果您通过IFD访问CRM

,则无法设置NetworkCredential类
var credentials = new ClientCredentials();
credentials.UserName.UserName = "username";
credentials.UserName.Password = "password";

var organizationUri = new Uri("https://externaluri");
var organizationServiceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
organizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());