1

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

Have a workaround in the form of powershell, please help to convert in C#

 $atts = $xml.GetElementsByTagName('attribute');
                    foreach($att in $atts)
                    {
                        if($att.ParentNode.HasAttribute('alias'))
                        {
                            $attName = $att.ParentNode.GetAttribute('alias') + "." + $att.name
                             Write-Output $att.ParentNode.GetAttribute('alias') + "." + $att.name
                        }
                        else
                        {
                            $attName = $att.name
                             Write-Output $att.name
                        }
                        Add-Member -InputObject $psobj -MemberType NoteProperty -Name $attName -Value $null
                        Add-Member -InputObject $psobj -MemberType NoteProperty -Name ($attName + "_Property") -Value $null
                    }
harshu288
  • 141
  • 1
  • 12

0 Answers0