Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Not working Retrieved Data from CRM using FetchXML with C#

Posted on by Microsoft Employee

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

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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.

  • Suggested answer
    Nflannery Profile Picture
    Nflannery 360 on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Not working Retrieved Data from CRM using FetchXML with C#

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,104 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans