Dynamics CRM 2015/Online 2015 更新プログラム 階層化の新機能 その2

(※ 2015 年 2 月 27 日 高度な検索の条件それぞれが FetchXML のどのクエリに対応するか追記)

みなさん、こんにちは。

前回に引き続き、Dynamics CRM 2015/Online 2015 更新プログラムの階層化に関する新機能を紹介します。
前回の記事を見られていない方は合わせてご確認ください。
- Dynamics CRM 2015/Online 2015 更新プログラム 階層化の新機能 その1

Dynamics CRM 2015 更新プラグラムでは階層構造を検索する機能が追加されています。
この機能は開発者向けとエンドユーザー向けの2つの機能に分類出来ます。

- エンドユーザー向け機能
- 開発者向け機能

エンドユーザー向け機能

高度な検索で階層構造をクエリーする為に、「未満」と「以上」の新しいオペレーターが使用出来るようになりました。
それぞれ、指定した値に一致するレコードの「未満」、「以上」で階層構造が検索されます。

hierarchy2_03ma

取引先企業エンティティでは、取引先企業の親会社というフィールドで参照できます。このフィールド名は階層設定で使用された 1:N の自己参照関係の名前です。

尚、「未満」は  FetchXML クエリの「Under」、「以上」は 「Not-Under」 の条件と同等になります。以下で紹介する FetchXML を 利用すると、この 2 つの
クエリ以外にもより柔軟なクエリを実行することが可能です。

開発者者向け機能

FetchXML

ConditionOperator

説明

above

Above

自己レコード以外のすべての親レコードを返す

eq-or-above

AboveOrEqual

自己レコードと、その上のすべての親レコードを返す

under

Under

自己レコード以外のすべての子レコードを返す

eq-or-under

UnderOrEqual

自己レコードと、その下のすべての子レコードを返す

not-under

NotUnder

自己レコード以外のすべての子レコードを除外する (詳細は後ほど説明があります)

eq-owneduseroruserhierarchy

OwnedByMeOrMyReports

階層セキュリティ モデル (*1) が使用されている場合、現在のユーザーまたはユーザーのレポート階層を等しくします。

eq-useroruserhierarchyandteams

OwnedByMeOrMyReportsAndTeams

階層セキュリティ モデル (*1) が使用されている場合、現在のユーザーとそのチームまたはユーザーのレポート階層とそのチームを等しくします。

階層化機能向けに追加された FetchXML オペレーター

(*1) 階層セキュリティモデルについては別の回で紹介します。

Above, AboveOrEqual, Under, UnderOrEqual, NotUnder オペレーターを使用する階層クエリーはリソース消費制限の為、上限が既定では 100 階層に設定されています。この制限は管理者用の PowerShell で変更する事が出来ます(詳細は Windows PowerShell を使用した展開の管理を参照下さい)。

FetchXMLクエリの説明

例として以下の階層構造を使用したクエリの動作を説明します。

image

NotUnder - 自己レコード以外のすべての子レコードを除外する

NotUnder は他のオペレーターと組み合わせると動作が理解しやすい為、eq-or-under, under, not-under の順で 3 段階に分けて説明します。

eq-or-under : アドベンチャーワークス

このクエリーではアドベンチャーワークスを含め、子レコードがすべて返ります。

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="account">
<attribute name="name" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="accountid" operator="eq-or-under"
value="{7696AC99-3E76-E411-810A-E83935C2E3BA}" />
</filter>
</entity>
</fetch>

実行結果

hierarchy2_05

under : ファブリカム

このクエリーではファブリカム以外のすべての子レコードが返ります。

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="account">
<attribute name="name" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="accountid" operator="under"
value=”{7896AC99-3E76-E411-810A-E83935C2E3BA}" />
</filter>
</entity>
</fetch>

実行結果

hierarchy2_06

eq-or-under : アドベンチャーワークス と
not-under : ファブリカム の組み合わせ

このクエリーではアドベンチャーワークスを含むすべての子レコードの中から、ファブリカムの下のレコードが除外された結果が返ります。

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="account">
<attribute name="name" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and">
      <condition attribute="accountid" operator="eq-or-under"
value="{7696AC99-3E76-E411-810A-E83935C2E3BA}" />
<condition attribute="accountid" operator="not-under"
value=”{7896AC99-3E76-E411-810A-E83935C2E3BA}" />
</filter>
</entity>
</fetch>

実行結果

hierarchy2_07

rowaggregate - 階層構造で一致するレコード数の取得

条件に一致する子レコードが何件あるか件数を取得するには、FetchXML の rowaggregate 属性を使用します。この属性の値が CountChildren に設定されている場合、この属性の値は EntityCollection に含まれる合計の子レコード数を表します。

under : アドベンチャーワークス
rowaggregate CountChildren

以下のクエリーは AccountChildlen 集計値を持ち、「アドベンチャーワークス」が親であるレコードが持つ子レコードの件数を返します。

<fetch distinct='false' no-lock='false' mapping='logical'>
  <entity name='account'>
    <attribute name='name' />
    <attribute name='accountid' />
     <attribute name='accountid' rowaggregate='CountChildren' alias='AccountChildren'/>
<filter type='and'>
        <condition attribute='accountid' operator='under'
value='{7696AC99-3E76-E411-810A-E83935C2E3BA}' />
      </filter>
  </entity>
</fetch>

実行結果

hierarchy2_08

OData Endpoint について

階層構造クエリーをサポートする為の変更は行われていません。

まとめ

階層構造を検索する為の機能が追加され、階層構造が扱いやすくなりました。是非お試し下さい。

- Kensuke Kitazawa

参照情報
Query hierarchical data
https://msdn.microsoft.com/en-us/library/dn817893.aspx