Hi,
I will try to retrieve data from CRM using Fetch XML with C# but not working.please check below code and mention what wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System.Net;
using System.ServiceModel.Description;
namespace Retrieve_Data_using_FetchXml
{
class Program
{
static void Main(string[] args)
{
IOrganizationService organizationService = null;
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "admin@deveshitsol.onmicrosoft.com";
clientCredentials.UserName.Password = "bsk@2018";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("deveshitsol.api.crm8.dynamics.com/.../Organization.svc"),
null, clientCredentials, null);
Console.WriteLine("Connection Established");
string fetch2 = @" <?xml version='1.0'?>
<fetch output - format = 'xml-platform' distinct = 'false' version = '1.0' mapping = 'logical'>
<entity name = 'icici_transaction'>
<attribute name = 'icici_name'/>
<attribute name = 'icici_transaction_name'/>
<attribute name = 'icici_customername'/>
<attribute name = 'icici_customeraccount'/>
<order attribute = 'icici_name' descending = 'false'/>
</entity>
</fetch>";
EntityCollection result = organizationService.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var c in result.Entities)
{
Console.WriteLine(c.Attributes["icici_name"]);
Console.WriteLine(c.Attributes["icici_customername"]);
Console.WriteLine(c.Attributes["icici_customeraccount"]);
Console.WriteLine(c.Attributes["icici_transaction_name"]);
}
}
}
}
*This post is locked for comments
In case the credentials in the code are actuals - Immediately change your password. You just exposed your whole org url, username, password just like that.
Try to debug your code, build/test your fetchxml query in xrmtoolbox plugins it will help you in long run.
Use try catch, check the result variable in VS watch.. then you will find the issue yourself.
I wouldn't use Fetch. Use queryexpression it generates the fetch for you and makes your code a lot tidier and concise.
This is the standard way to do this. See below
www.magnetismsolutions.com/.../dynamics_crm_2011_querying_data_with_queryexpression
Hi Bobby,
You can use the below code. Also please go through the below article as well to understand how to get/ set different type field in CRM. In your particular fetch, icici_customeraccount is a lookup field so you just can't get the value by c["icici_customeraccount"] hence when you run this code, it will show you EntityReference as text and not the actual value.
public static void ProcessWhoAmI() { var _credential = new ClientCredentials(); _credential.UserName.UserName = "<username>"; _credential.UserName.Password = "<password>"; var _organizationURI = "<org url>"; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (_service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null)) { var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='icici_transaction'> <attribute name='icici_name' /> <attribute name='icici_transaction_name' /> <attribute name='icici_customername' /> <attribute name='icici_customeraccount' /> <order attribute='icici_name' descending='false' /> </entity> </fetch>"; var results = _service.RetrieveMultiple(new FetchExpression(fetch)); Console.WriteLine(results.Entities.Count); foreach (var c in results.Entities) { Console.WriteLine(c.Contains("icici_name")? c["icici_name"]: "Not Available"); Console.WriteLine(c.Contains("icici_customername") ? c["icici_customername"] : "Not Available"); Console.WriteLine(c.Contains("icici_customeraccount") ? c["icici_customeraccount"] : "Not Available"); Console.WriteLine(c.Contains("icici_transaction_name") ? c["icici_transaction_name"] : "Not Available"); } Console.ReadLine(); } }
Hope this helps.
Hi,
As your fetchxml is contains <?xml version='1.0'?> remove this tag. you can test your fetchxml : http://msxrmtools.com/fetchxml
Thank you
Pankaj Kumar
If found useful, please mark the answer as verified
Hi ,
Can you please check your fetchxml format ?
It should be in above format:
string fetchxmlstring = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='incident'>
<attribute name='title' />
<attribute name='ticketnumber' />
<attribute name='createdon' />
<attribute name='incidentid' />
<attribute name='caseorigincode' />
<order attribute='title' descending='false' />
<filter type='and'>
<condition attribute='modifiedon' operator='today' />
</filter>
</entity>
</fetch>";
Hope this help you. !!!!
Hi , can you please try to debug.
issue can be one of the below:
1. you are not successfully connected and organisation service might be null hence not able to retrieve records
2. check if this fetch actually returns the value : http://msxrmtools.com/fetchxml you can test here.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,104 Most Valuable Professional
nmaenpaa 101,156