fetchxml結果データをどのように処理しますか?
-
06-07-2019 - |
質問
crmService.Fetch(fetchXml)を呼び出した後、結果データを処理する最良の方法がわからないため、fetchxmlの使用を避けました。いくつかの状況で、XDocumentとLINQを使用して、次のようなこのデータ構造からデータを取得しました。
XDocument resultset = XDocument.Parse(_service.Fetch(fetchXml));
if (resultset.Root == null || !resultset.Root.Elements("result").Any())
{
return;
}
foreach (var displayItem in resultset.Root.Elements("result").Select(item => item.Element(displayAttributeName)).Distinct())
{
if (displayItem!= null && displayItem.Value != null)
{
dropDownList.Items.Add(displayItem.Value);
}
}
fetchxmlの結果データを処理して、簡単に使用できるようにする最良の方法は何ですか。これらのレコードをASP.NETデータグリッドに渡すなどのアプリケーションは非常に便利です。
解決
私は通常、まさにこの理由でFetchXMLを避けています。 RetrieveMultipleを使用して、厳密に型指定されたBusinessEntityオブジェクトを取得し、基本的に同じことを実行できます。
ただし、FetchXMLを使用する場合は、このサンプルでカバーできます。
他のヒント
FetchXMLの柔軟性を楽しんでいるので、グリッドやリピーターなどへのバインドに使用するデータテーブルを返す次の関数を開発しました。
/// <summary>
/// Takes a CRM FetchXML query and returns a DataTable
/// </summary>
/// <param name="fetchXml">The FetchXML query</param>
/// <param name="requiredFields">A array of columns you'd expect returned. This is required as if there is no data for a field/column CRM will not return it which could impact databinding</param>
/// <returns>A datatable containing the results of the FetchXML</returns>
public static DataTable FetchXML2DataTable(string fetchXml, string[] requiredFields)
{
CrmService tomService = new CrmService();
tomService = CrmWebService;
string result = tomService.Fetch(fetchXml);
DataSet ds = new DataSet();
System.IO.StringReader reader = new System.IO.StringReader(result);
ds.ReadXml(reader);
DataTable dt = ds.Tables[1];
//check all required columns are present otherwise add them to make life easier for databinding at the top level
//caused by CRM not returning fields if they contain no data
foreach (string field in requiredFields)
{ //Check for column names and nested tables
if ((dt.Columns.IndexOf(field) < 0) && (dt.DataSet.Tables.IndexOf(field) <0))
{
//Add column to datatable even though it is empty for reason stated above
dt.Columns.Add(new DataColumn(field));
}
}
return dt;
}
requiredFields文字列配列は、結果セットにその列のデータが含まれていない場合は列が返されないためです。ただし、datagridsなどにバインドする正確な理由で列を配置する必要がある場合があります。
CrmServiceは、Webサービスを初期化するシングルトンクラスです。
これがあなたの役に立つことを願っています。
QueryExpressionを使用すると、多対多のエンティティを照会できず、複数のエンティティから属性を一度に取得できないため、FetchXMLを使用する必要があります。
残念なことに、LynqToCRMのcodeplexプロジェクトは、ダイナミックcrmのlinqプロバイダーを含むCRMのSDK 4.0.12のリリースで廃止されました(新しいリリースなしで1年ですが、Microsoftのリリースよりも良い実装のようです)。 、しかし、私はこの新しいリリースに関する記事を読みましたが、あまり良くありませんが、「実装が悪い」ようです。多くの制限(強制キャッシュなど)があります。
FetchXMLの結果をリードするためにLinqToXMLとDataSetを使用している多くの人々を見ていますが、それを処理する最善の方法は言えません。これについてどう思いますか?
Christophe Trevisani Chavey。
LinqtoCRMを使用することもできます。これにより、XML解析が処理されます。 http://codeplex.com/linqtocrm
fetchxmlを使用し、BusinessEntityTypeのリターンセットを取得する場合は、 FetchXmlToQueryExpressionメソッドを使用して、fetchxmlからクエリ式を取得し、RetrieveMultipleメソッドでクエリ式を適用します
FetchXmlToQueryExpressionResponse qe = (FetchXmlToQueryExpressionResponse) service.Execute(fetch);
逆メソッドQueryExpressiontoFetchXmlも存在することに注意してください