Dynamics CRM 2011 Xrm.Page サンプルライブラリ紹介 その 5
みなさん、こんにちは。
今回は Xrm.Page.data オブジェクトのサンプルを紹介していきます。
サンプルを確認するにあたり準備があるため、こちらの記事を
ご覧いただき、サンプルのインストールを行ってください。
Xrm.Page.data オブジェクト
サンプルの前に、Xrm.Page.data オブジェクトを簡単に説明します。
Xrm.Page オブジェクトモデルでは、画面とデータを別のオブジェクトと
して扱います。画面は Xrm.Page.ui で、データ側が Xrm.Page.data と
なります。これにはいくつかメリットと理由がありますが、その 1 つには
Dynamics CRM 2011 では、フォームに同じフィールドを繰り返し配置
できるようになったことがあげられます。
ただしフォーム上にないデータは取得されないため、最終的には
Xrm.Page.ui と Xrm.Page.data は control オブジェクトに辿り
つきます。つまり Xrm.Page.ui.controls オブジェクトと
Xrm.Page.data.entity.attributes.controls は同じものです。
サンプル紹介
今回紹介するサンプルは、以下のフォルダにあります。
SDK\SampleCode\JS\FormScripts\SDK.AttributesSamples.js
SDK\SampleCode\JS\FormScripts\SDK.AttributesCollectionSamples.js
フィールドの値/状態確認サンプル
初期値、現在選択された値、値が変更されたかなどの状態を取得できます。
[showInitialValue 関数]
オプションセットの初期値を取得して表示します。
showInitialValue: function ()
{
// 初期値一覧を表示する HTML を作成
var html = "<html><head><title>OptionSet and Boolean Default Values</title>";
html += "<style type=\"text/css\">body { font-family:Calibri;}";
html += "table {border:1px solid gray; border-collapse:collapse;}";
html += "th {text-align:left; border:1px solid gray;}";
html += "td {border:1px solid gray;}</style>";
html += "</head><body>";
// getBooleanAndOptionSetAttributes サンプル関数でフィールドの値を取得
html += SDK.AttributeSamples.getBooleanAndOptionSetAttributes();
html += "</body></html>";
var myWindow = window.open("", "_blank");
myWindow.document.open();
myWindow.document.write(html);
myWindow.document.close();
},
[getBooleanAndOptionSetAttributes 関数]
ロードされているフィールドの初期値を取得します。
getBooleanAndOptionSetAttributes: function ()
{
var fields = "<table><thead><th>Name</th><th>Initial Value</th></thead><tbody>";
// Xrm.Page.data.entity.attributes の get メソッドと
// IsBooleanOrOptionSet サンプル関数で、ロードされた
// オプションセットのフィールドを取得
var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isBooleanOrOptionSet);
for (var i in attributes)
{
var attribute = attributes[i];
// Xrm.Page.data.entity.attribute の getName と getInitialValue
// メソッドで、名前を初期値を取得
fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getInitialValue() + "</td></tr>";
}
fields += "</tbody></table>";
return fields;
},
[isBooleanOrOptionSet 関数]
フィールドのタイプがオプションセットか確認
isBooleanOrOptionSet: function (attribute, index)
{
var result = false;
// Xrm.Page.data.entity.attribute の getAttributeType メソッドで
// フィールドのタイプを取得
var type = attribute.getAttributeType();
if (type == "boolean" || type == "optionset")
{ result = true; }
return result;
},
[showAttributeSelectedOption 関数]
オプションセットで現在選択されている値を取得して表示します。
showAttributeSelectedOption: function ()
{
var html = "<html><head><title>getOption</title>";
html += "<style type=\"text/css\">body { font-family:Calibri;}";
html += "table {border:1px solid gray; border-collapse:collapse;}";
html += "th {text-align:left; border:1px solid gray;}";
html += "td {border:1px solid gray;}</style>";
html += "</head><body>";
// getSelectedOption サンプル関数で、現在の値を取得
html += SDK.AttributeSamples.getSelectedOption();
html += "</body></html>";
var myWindow = window.open("", "_blank");
myWindow.document.open();
myWindow.document.write(html);
myWindow.document.close();
},
getSelectedOption: function ()
{
var fields = "<table><thead><th>Name</th><th>Selected JSON option</th></thead><tbody>";
// IsOptionSet サンプル関数を利用してオプションセットを取得
var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet);
for (var i in attributes)
{
var attribute = attributes[i];
// 現在の値を取得
var attributeValue = attribute.getValue();
fields += "<tr><td>" + attribute.getName() + "</td><td>";
// SDK.AttributeSamples.showJSONOption(attribute)
if (attributeValue != null)
{
// 現在の値より選択されているオプションを取得
var option = attribute.getOption(attributeValue);
fields += " {";
fields += " text:\"" + option.text + "\" , value:\"" + option.value + "\"";
fields += " }";
}
else
{ fields += "No selected value"; }
fields += "</td></tr>";
}
fields += "</tbody></table>";
return fields;
},
[isOptionSet 関数]
オプションセットか確認する。
isOptionSet: function (attribute, index)
{
return attribute.getAttributeType() == "optionset";
},
[showIsDirty 関数]
値が初期値から変更されているか確認します。
showIsDirty: function ()
{
// IsDirty の結果を表示する HTML を作成
var html = "<html><head><title>Show IsDirty</title>";
html += "<style type=\"text/css\">body { font-family:Calibri;}";
html += "table {border:1px solid gray; border-collapse:collapse;}";
html += "th {text-align:left; border:1px solid gray;}";
html += "td {border:1px solid gray;}</style>";
html += "</head><body>";
// getIsDirty サンプル関数で値が変更されたか確認
html += SDK.AttributeSamples.getIsDirty();
html += "</body></html>";
var myWindow = window.open("", "_blank");
myWindow.document.open();
myWindow.document.write(html);
myWindow.document.close();
},
[getIsDirty 関数]
値が初期値から変更されている確認
getIsDirty: function ()
{
var html = "<table><thead><th>Attribute Name</th><th>IsDirty</th></thead><tbody>";
// Xrm.Page.data.entity.attributes の get メソッドで
// ロードされているフィールドを取得
var attributes = Xrm.Page.data.entity.attributes.get()
for (var i in attributes)
{
var attribute = attributes[i];
html += "<tr><td>" + attribute.getName() +
// getIsDirty メソッドで変更されたか確認
"</td><td>" + attribute.getIsDirty() +
"</td></tr>";
}
html += "</tbody></table>";
return html;
},
特定のフィールドの条件を確認/変更するサンプル
フィールドの設定はカスタマイズで変更可能ですが、スクリプトからも
ある程度の操作が可能です。
[showRequiredLevel 関数]
各フィールドの現在の要求レベルを確認します。
showRequiredLevel: function ()
{
// 要求レベルの一覧を表示する HTML を作成
var html = "<html><head><title>Required Levels </title>";
html += "<style type=\"text/css\">body { font-family:Calibri;}";
html += "table {border:1px solid gray; border-collapse:collapse;}";
html += "th {text-align:left; border:1px solid gray;}";
html += "td {border:1px solid gray;}</style>";
html += "</head><body>";
// getRequiredLevel サンプル関数で要求レベルを取得
html += SDK.AttributeSamples.getRequiredLevel();
html += "</body></html>";
var myWindow = window.open("", "_blank");
myWindow.document.open();
myWindow.document.write(html);
myWindow.document.close();
},
[getRequiredLevel 関数]
フィールドの要求レベルを取得します。
getRequiredLevel: function ()
{
var fields = "<table><thead><th>Name</th><th>Precision</th></thead><tbody>";
// Xrm.Page.data.entity.attributes の get メソッドでフィールドを取得
var attributes = Xrm.Page.data.entity.attributes.get()
for (var i in attributes)
{
var attribute = attributes[i];
// Xrm.Page.data.entity.attribute の getRequiredLevel メソッドで
// 要求レベルを確認
var requiredLevel = attribute.getRequiredLevel();
if (requiredLevel != "none")
{
fields += "<tr><td>" + attribute.getName() +
"</td><td>" + requiredLevel +
"</td></tr>";
}
}
fields += "</tbody></table>";
return fields;
},
[makeOptionSetsRequired 関数]
オプションセットを全て必須にするサンプル関数です。
makeOptionSetsRequired: function ()
{
// isOptionSet サンプル関数と Xrm.Page.data.entity.attributes の get
// メソッドで、オプションセットフィールドを取得
var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet);
for (var i in attributes)
{
// Xrm.Page.data.entity.attribute の setREquiredLevel で
// 必須を設定
attributes[i].setRequiredLevel("required");
}
},
フィールドレベルセキュリティ情報取得のサンプル
フィールドに関連する情報として、フィールドレベルセキュリティの情報も
取得することが可能です。
[showUserPrivileges 関数]
フィールドレベルセキュリティーの情報を取得、表示します。
showUserPrivileges: function ()
{
// フィールドレベルセキュリティ情報を表示する HTML を作成
var html = "<html><head><title>User Privilege</title>";
html += "<style type=\"text/css\">body { font-family:Calibri;}";
html += "table {border:1px solid gray; border-collapse:collapse;}";
html += "th {text-align:left; border:1px solid gray;}";
html += "td {border:1px solid gray;}</style>";
html += "</head><body>";
// getUserPrivileges サンプル関数でフィールドレベルセキュリティ
// の情報を取得
html += SDK.AttributeSamples.getUserPrivileges();
html += "</body></html>";
var myWindow = window.open("", "_blank");
myWindow.document.open();
myWindow.document.write(html);
myWindow.document.close();
},
[getUserPrivileges 関数]
getUserPrivileges: function ()
{
var fields = "<table><thead><th>Name</th><th>User Privileges</th></thead><tbody>";
var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes)
{
var attribute = attributes[i];
// Xrm.Page.data.entity.attribute の getUserPrivilege メソッドで
// フィールドレベルセキュリティの結果を取得。
var privileges = attribute.getUserPrivilege();
// getName、canRead、canUpdate、canCreate メソッドで
// 名称とそれぞれの権限を取得
fields += "<tr><td rowspan=3>" + attribute.getName() + "</td>"
fields += "<td> canRead: " + privileges.canRead + "</td></tr>";
fields += "<tr><td> canUpdate: " + privileges.canUpdate + "</td></tr>";
fields += "<tr><td> canCreate: " + privileges.canCreate + "</td></tr>";
fields += "</tr>";
}
fields += "</tbody></table>";
return fields;
},
まとめ
Xrm.Page.data を利用すれば、フィールド側の観点から操作が行えます。
実装したい操作が Xrm.Page.ui に無い場合には、Xrm.Page.data を確認
してください。
尚、Xrm.Page.data で取得できるフィールドは、フォーム上に設置されている
コントロールに対応するもののみです。よって特定のフィールドの情報を
スクリプトで利用したいが、フォームには表示したくない場合には、フォーム
にはコントロールをおいておき、非表示設定にしてください。
次回は最終回で、コンテキストに関連するサンプルを紹介します。
‐ Dynamics CRM サポート 中村 憲一郎