Dynamics crm 如何在Microsoft Dynamics CRM 2011中查询商机

Dynamics crm 如何在Microsoft Dynamics CRM 2011中查询商机,dynamics-crm,dynamics-crm-2011,Dynamics Crm,Dynamics Crm 2011,我正在尝试从Microsoft Dynamics CRM 2011查询商机信息 你知道为什么我总是收到一个错误吗 如果我在浏览器中使用URL,它似乎可以工作 Uri organizationUri = new Uri("/XRMServices/2011/OrganizationData.svc"); Uri homeRealmUri = null; ClientCredentials credentials = new ClientCredentials(); credentials.Wind

我正在尝试从Microsoft Dynamics CRM 2011查询商机信息

你知道为什么我总是收到一个错误吗

如果我在浏览器中使用URL,它似乎可以工作

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();
}

您是通过内部网还是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());