简体   繁体   中英

CRM 2013 C# PlugIn - Fetch XML Query

I have a Grid on my entity page Orders, users can add products to the entity grid

For my plugin I need to know what products have been inserted into the grid so that I can then produce a CSV file

I've tried using a FetchXML query to retrieve the data which is as follows;

string fetchxml = @"
<fetch mapping= 'logical'>
   <entity name ='product'>
      <attribute name = 'name'/>
   </entity>
 </fetch> ";

EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));foreach(var c in result.Entities){
 if (result != null && result.Entities.Count > 0)
   {
     List<string> _product = new List<string>();
     foreach (Entity _entity in result.Entities)
       {
         _product.Add(_entity.Attributes["name"].ToString());
       }
string CSVFile = string.Join(",", _product.ToArray());string AddressPath = "FM-OR" + "_";
string AddressSavePath = @"\\fm\CRMdata\maesteg\" + AddressPath + ".csv";
System.IO.File.WriteAllText(AddressSavePath, CSVFile.ToString());
}
}

The code does produce the required CSV file that I need however it's selected every record in the entity products instead of the required ones in the grid. Any suggestions on how I sort this problem?

Thanks, Shaun

You need to use Link-Entity and Filter in your fetch query. Check the examples below:

Sample: Use aggregation in FetchXML

Fetch Xml should be something like this:

Guid orderId = (Guid)((Entity)context.InputParameters["Target"]).Id;

<?xml version="1.0"?>
    <fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
    <entity name="product">
        <attribute name="name"/>
        <attribute name="productnumber"/>
        <attribute name="subjectid"/>
        <attribute name="statecode"/>
        <attribute name="productid"/>
        <order descending="false" attribute="productnumber"/>
        <link-entity name="salesorderdetail" alias="aa" to="productid" from="productid">
            <link-entity name="salesorder" alias="ab" to="salesorderid" from="salesorderid">
                <filter type="and">
                    <condition attribute="salesorderid" operator="eq" value=orderId />
                </filter>
            </link-entity>
       </link-entity>
    </entity>
</fetch>

"To some value" is the id of the entity where the plugin is triggered from check this link: http://msdn.microsoft.com/en-us/library/gg309673.aspx

your code should look like:

@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
 <entity name='product'>
     <attribute name = 'name'/>
     <order attribute='name' descending='false' />
     <filter type='and'>
         <condition attribute='productid' operator='eq' value=" + context.InputParameters["Target"].id + @" />
     </filter>
     <link-entity name='order' from='projectid' to='project' alias='Order'>
       ........
     </link-entity>
 </entity>
</fetch>"

bear in mind that probably the string concatenation that i did is wrong, but i had to do it like that to highlight the syntax :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM