web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Not working Retrieved Data from CRM using FetchXML with C#

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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.

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    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. !!!!

  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

    https://www.magnetismsolutions.com/blog/roshanmehta/2012/07/19/setting_values_programmatically_via_the_crm_2011_sdk

    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.

  • Suggested answer
    Nflannery Profile Picture
    360 on at

    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

    msdn.microsoft.com/.../gg328300.aspx

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans