开发者

How to query Opportunities in Microsoft Dynamics CRM 2011

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 = (IOrganization开发者_如何学JAVAService)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();
}


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜