We're on Dynamics CRM 2016 On-Premise. I'm trying to execute a FetchXML from a Dynamics CRM Console App.
I can connect successfully to our CRM.
I've tested the FetchXML successfully and returned results using XrmToolbox (https://www.xrmtoolbox.com/plugins/MscrmTools.FetchXmlTester)
But I keep getting System.NullReferenceException Object reference not set to an instance of an object at the step RetrieveMultiple that tries to execute the FetchXML... Any suggestion is greatly appreciated. Here's my code:
class Program
{
private const string GetSampleAccounts = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='new_myfield' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='new_myfield' operator='like' value='%888' />
</filter>
</entity>
</fetch>";
static void Main(string[] args)
{
try
{
var connectionString = @"AuthType=IFD;
Username=rri\myname;
Integrated Security=true;
Url=https://Mmycrm.com;
LoginPrompt=Auto";
CrmServiceClient conn = new CrmServiceClient(connectionString);
Console.WriteLine("Connection Successful!...");
IOrganizationService service;
service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
DataCollection<Entity> result = service.RetrieveMultiple(new FetchExpression(string.Format(GetSampleAccounts))).Entities; //GET ERROR HERE
if (result != null && result.Count == 1)
{
foreach (Entity account in result)
{
Console.WriteLine(account.Attributes["name"];
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Console.ReadKey();
}
Try this:
string connectionString = @"AuthType=AD;Url=https://Mmycrm.com";
using (var service = new CrmServiceClient(connectionString))
{
foreach (Entity account in service.RetrieveMultiple(new FetchExpression(GetSampleAccounts))
.Entities)
{
Console.WriteLine(account.Attributes["name"]);
}
}