locked
CRM Fetchxml returns with missing attributes for blank rows RRS feed

  • Question

  • Hello all,

    I am using fetchxml query to get results from CRM dynamics however if
    my fetchxml is having 6 attributes and query this I get  less than 6
    attributes, reason for this, if the row is having blank/null value then
    it does not return attribute name, I need the attribute names since I
    have written the code to get the attribute names and dynamically get
    data from attribute names.
    Hers is my code.

    Let says my fetchxml contains a string like this(Note: I 'm getting the string dynamically from view name, just showing the string)

    string fetchxml= @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
      <entity name='cdi_postedsurvey'>
        <attribute name='cdi_postedsurveyid' />
        <attribute name='cdi_name' />
        <attribute name='createdon' />
        <order attribute='cdi_name' descending='false' />
        <filter type='and'>
          <condition attribute='statecode' operator='in'>
            <value>1</value>
            <value>0</value>
          </condition>
          <condition attribute='cdi_postedsurveyid' operator='not-null' />
          <condition attribute='createdon' operator='on-or-after' value='2016-09-09' />
          <condition attribute='createdon' operator='on-or-before' value='2016-09-13' />
        </filter>
      </entity>
    </fetch>";
    
    EntityCollection _viewResult = _orgService.RetrieveMultiple(new FetchExpression(_fetchxml));
    
     foreach (var e in _viewResult .Entities)   **//Here the entity return less attributes because the row is blank**                     
                            {
                                if (_keycnt == 0)
                                {
                                    foreach (var keys in e.Attributes.Keys)
                                    {
                                        _csvData.AppendFormat(keys + ",");  //Get all the Attribute names in comma separted format.
                                    }
                                    _splitIntoColumn = _csvData.ToString().TrimEnd().Split(',');
                                }
                                _keycnt = 1;
                                _csvData.Remove(_csvData.ToString().LastIndexOf(","), 1);           //Removes comma from the end of the string
                                _csvData.AppendLine();                                              //Enters new line
                                for (int j = 0; j < _splitIntoColumn.Length - 1; j++)
                                {
                                    string _attributeName = _splitIntoColumn[j].ToString();
                              //Now get the values based on attribute name
                                    if (e.FormattedValues.Contains(_attributeName))
                                    {
                                        //append data in csv file
                                    }

    This is what I 'm getting in my CSV file wherein I have 4 attributes, but I 'm getting 3 because the first row is blank.

    cdi_postedsurveyid  createdon   logicalname
    A                   14-09-2011       sample
    B                   14-09-2011       sample

    This is what I want wherein cdi_name is included even if the first row is null

    cdi_name  cdi_postedsurveyid    createdon   logicalname
                        A                  14-09-2011       sample
    test data       B                  14-09-2011       sample
    test data       B                  14-09-2011       sample

    Note: Just for your information(not using this library) here the powershell library is fixed for Microsoft.Xrm.Data.PowerShell https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell/releases



    • Edited by harshu288 Friday, November 25, 2016 11:26 AM jgjgb
    Friday, November 25, 2016 10:54 AM

All replies

  • This is standard CRM behaviour, and there is no way to change it. There are 2 main workarounds:

    1. Ensure that the data returned always includes a record that has data in all attributes. This is often not practicable, but worth mentioning for completeness
    2. Get the attribute names by parsing the fetchxml. This is relatively straightforward if all attributes are on the main entity, but you should ideally cover attributes that are part of a link-entity

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, November 25, 2016 4:03 PM
    Moderator
  • try this:-

    if (e.Attribute.Contains(_attributeName)) { //Put value here

    } else { //put a null value here }



    Regards Faisal

    Wednesday, November 30, 2016 11:19 AM