如何在 Microsoft Dynamics CRM 2011 中查询机会
我正在尝试从 Microsoft Dynamincs CRM 2011 查询机会信息。
知道为什么我不断收到 401 未经授权 错误吗?
如果我在浏览器中使用 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();
}
I am trying to query Opportunity information from Microsoft Dynamincs CRM 2011.
Any idea why I keep getting a 401 Unauthorized error?
If I use the URL in the browser it seems to work.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否通过 Intranet 或 IFD 访问 CRM?我认为问题在于您设置凭据的方式。
如果您通过 IFD 访问 CRM,则设置 NetworkCredential 类将不起作用
Are you accessing you CRM on the intranet or IFD? I think the problem is the way you set up credentials.
Setting up NetworkCredential class will not work if you are accessing your CRM through IFD