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)

Fetch XML passing parameters in C#?

(0) ShareShare
ReportReport
Posted on by

All,

I need help with passing a c# variable into my fetch xml code. Can someone tell me if this is possible?

Using this sample code below, how can I pass the VALUE argument into the fetch xml for the ORDER CLOSED filter criteria so it's not hard coded?

Sample code:

int value = 1;  

string fetchXml =
                          @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='salesorder'> 
                                                 <attribute name='salesorderid'/>
                                                <attribute name='ordernumber'/>
                                                <attribute name='salesorderid' />  
                                                <attribute name='customerid'/>
                                                <attribute name='statuscode'/> 
                                                <attribute name='totalamount'/>  
                                                <attribute name='salesorderid' />
                                                <attribute name='billto_name' /> 
                                                <attribute name='billto_city' /> 
                                                <attribute name='billto_stateorprovince' /> 
                                                <attribute name='billto_postalcode' /> 
                                                <attribute name='billto_telephone' />    
                                                <attribute name='description' />   
                                                <attribute name='shipto_contactname' /> 
                                                <attribute name='shipto_telephone' /> 
                                                <attribute name='shipto_fax' /> 
                                                <attribute name='shipto_line1' />
                                                <attribute name='shipto_line2' />
                                                <attribute name='shipto_city' />
                                                <attribute name='shipto_stateorprovince' />
                                                <attribute name='shipto_postalcode' />
                                                <attribute name='shipto_country' />  
                                                <attribute name='totalamount' />   
                                      <filter type='and'>
                                         <condition attribute='orderclosed' operator='eq' value='1' />  
                                           </filter> 
  </entity>
</fetch>";


// passed into the filter criteria rather than hard coding.
int val = 1; 
<filter type='and'> <condition attribute='orderclosed' operator='eq' value='1' /> </filter>

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Swetha Poojary Salaskar Profile Picture
    326 on at

    Hi,

    You can pass the value like this

     "'+your variable name+'"

    Example:

    int value = 1;  
    
    string fetchXml =

    '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'+
    '<entity name="salesorder"> '+
    '<attribute name="salesorderid"/>'+
    '<attribute name="ordernumber"/>'+
    '<attribute name="salesorderid" />'+
    '<attribute name="customerid"/>'+
    '<attribute name="statuscode"/> '+
    '<attribute name="totalamount"/> '+
    '<attribute name="salesorderid" />'+
    '<attribute name="billto_name" /> '+
    '<attribute name="billto_city" /> '+
    '<attribute name="billto_stateorprovince" />'+
    '<attribute name="billto_postalcode" /> '+
    '<attribute name="billto_telephone" /> '+
    '<attribute name="description" /> '+
    '<attribute name="shipto_contactname" /> '+
    '<attribute name="shipto_telephone" /> '+
    '<attribute name="shipto_fax" /> '+
    '<attribute name="shipto_line1" />'+
    '<attribute name="shipto_line2" />'+
    '<attribute name="shipto_city" />'+
    '<attribute name="shipto_stateorprovince" />'+
    '<attribute name="shipto_postalcode" />'+
    '<attribute name="shipto_country" />'+
    '<attribute name="totalamount" />'+
    ' <filter type="and">'+
    '<condition attribute="orderclosed" operator="eq" value=""+value+"" /> '+
    '</filter> '+
    ' </entity>'+
    '</fetch>';

  • Community Member Profile Picture
    on at

    It's not working. I tried both int value = 1 and bool value = true;

    here's the error

    An exception System.FormatException was thrown while trying to convert input value '+value+' to attribute 'salesorder.ordercompleted'. Expected type of attribute value: System.Boolean. Exception raised: The string '+value+' is not a valid Boolean value.

    code:

    int value = 1;  
    
    string fetchXml =
                              @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
      <entity name='salesorder'> 
                                                     <attribute name='salesorderid'/>
                                                    <attribute name='ordernumber'/>
                                                    <attribute name='salesorderid' />  
                                                    <attribute name='customerid'/>
                                                    <attribute name='statuscode'/> 
                                                    <attribute name='totalamount'/>  
                                                    <attribute name='salesorderid' />
                                                    <attribute name='billto_name' /> 
                                                    <attribute name='billto_city' /> 
                                                    <attribute name='billto_stateorprovince' /> 
                                                    <attribute name='billto_postalcode' /> 
                                                    <attribute name='billto_telephone' />    
                                                    <attribute name='description' />   
                                                    <attribute name='shipto_contactname' /> 
                                                    <attribute name='shipto_telephone' /> 
                                                    <attribute name='shipto_fax' /> 
                                                    <attribute name='shipto_line1' />
                                                    <attribute name='shipto_line2' />
                                                    <attribute name='shipto_city' />
                                                    <attribute name='shipto_stateorprovince' />
                                                    <attribute name='shipto_postalcode' />
                                                    <attribute name='shipto_country' />  
                                                    <attribute name='totalamount' />   
                                          <filter type='and'>
                                             <condition attribute='orderclosed' operator='eq' value=""+value+""  />  />  
                                               </filter> 
      </entity>
    </fetch>";

     

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Easy enough, just put a placeholder in the string then do a String.Format on it like this:

    string fetchXml =
                              @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
      <entity name='salesorder'> 
                                                     <attribute name='salesorderid'/>
                                                    <attribute name='ordernumber'/>
                                                    <attribute name='salesorderid' />  
                                                    <attribute name='customerid'/>
                                                    <attribute name='statuscode'/> 
                                                    <attribute name='totalamount'/>  
                                                    <attribute name='salesorderid' />
                                                    <attribute name='billto_name' /> 
                                                    <attribute name='billto_city' /> 
                                                    <attribute name='billto_stateorprovince' /> 
                                                    <attribute name='billto_postalcode' /> 
                                                    <attribute name='billto_telephone' />    
                                                    <attribute name='description' />   
                                                    <attribute name='shipto_contactname' /> 
                                                    <attribute name='shipto_telephone' /> 
                                                    <attribute name='shipto_fax' /> 
                                                    <attribute name='shipto_line1' />
                                                    <attribute name='shipto_line2' />
                                                    <attribute name='shipto_city' />
                                                    <attribute name='shipto_stateorprovince' />
                                                    <attribute name='shipto_postalcode' />
                                                    <attribute name='shipto_country' />  
                                                    <attribute name='totalamount' />   
                                          <filter type='and'>
                                             <condition attribute='orderclosed' operator='eq' value='{0}' />  
                                               </filter> 
      </entity>
    </fetch>";
    
    
    fetchXml = String.Format(fetchXml, value);

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

      Aiden

  • Suggested answer
    Community Member Profile Picture
    on at

    I can't find "orderclosed" attribute on entity salesorder. Are you sure is it correct name?

    Try to build your fetch in advanced find, set condition for this field and download fetch. It will allow you to see correct format for field.

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    You can't use " + value + " because you're combining different types. You can either do

    + value.ToString() +,  or a string format.  Additionally, this won't work:

    value=""+value+""

    because you don't have the single quotes, would need to be

    value='"+value+"' (with ' instead of the outer ")

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Good catch Uriy,

    To check the status you need to filter on statuscode:

         <condition attribute="statuscode" operator="eq" value="100001" />

  • Community Member Profile Picture
    on at

    You are the man Aiden!!! Thanks

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