CRM Fetchxml返回缺少空行的属性

时间:2016-11-25 10:13:45

标签: c# dynamics-crm-2011

我正在使用fetchxml查询来获取CRM动态的结果但是如果我的fetchxml有6个属性并且查询这个我得到的属性少于6个,原因是,如果行有空/空值则它不会返回属性名称,我需要属性名称,因为我编写了代码来获取属性名称并从属性名称动态获取数据。 她是我的代码。

假设我的fetchxml包含这样的字符串(注意:我从视图名称动态获取字符串,只显示字符串

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
                                }

这是我在我的CSV文件中获得的,其中我有4个属性,但我得到3,因为第一行是空白的。

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

这就是我想要的,即使第一行为空,也包括cdi_name

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

注意:仅为了您的信息(不使用此库),这里修复了powershell库,用于Microsoft.Xrm.Data.PowerShell https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell/releases

以PowerShell的形式提供解决方法,请帮助转换为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
                    }

0 个答案:

没有答案