简体   繁体   中英

CRM RetrieveMultipleResponse not returning any results

I am attempting to make a console application to retrieve results using the Dynamics CRM SDK and C# but I can't seem to get any results from my queries. I am able to see that I am connected to the server, but any QueryExpression I try to make seems to come back with nothing, even if I set it with no filter. Even when using an example from the documentation that we have corresponding values to I end up empty handed. My code is:

        CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString);
        Console.WriteLine(crmSvc.IsReady);
        //Display the CRM version number and org name that you are connected to.
        Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}",
        crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

        QueryExpression userSettingsQuery = new QueryExpression("contact");
        userSettingsQuery.ColumnSet.AllColumns = true;
        var retrieveRequest = new RetrieveMultipleRequest()
        {
            Query = userSettingsQuery
        };
        EntityCollection EntCol = (crmSvc.ExecuteCrmOrganizationRequest(retrieveRequest) as RetrieveMultipleResponse).EntityCollection;
        foreach (var a in EntCol.Entities)
        {
            Console.WriteLine("Account name: {0} {1}", a.Attributes["firstname"], a.Attributes["lastname"]);
        }
        Console.Write(crmSvc.LastCrmError);
        Console.Write(crmSvc.LastCrmException);
        Console.ReadLine();

It returns no errors, and shows True for the connection, and I can't seem to find where to start to troubleshoot from here.

Your code works fine for me so it may be that the user you are connected as doesn't have permission to read contact records or there are no contact records in your CRM organization.

Also, I know you are trying to piece together a sample, but I put together a more straight forward version of your code below.

    var crmSvc = new CrmServiceClient(<CONNECTION STRING>);

    var contactQuery = new QueryExpression("contact")
    {
        ColumnSet = new ColumnSet("firstname", "lastname")
    };
    var contacts = crmSvc.RetrieveMultiple(contactQuery).Entities;
    foreach(var contact in contacts)
    {
        Console.WriteLine("Contact name: {0} {1}", contact.GetAttributeValue<String>("firstname"), contact.GetAttributeValue<String>("lastname"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM