Dynamics CRM 2013/Fall '13 ユーザー定義アクション その 2

みなさん、こんにちは。

今回は前回に続き、SDK から操作を実行する紹介します。
前回からの続き物となりますのでご注意ください。

Dynamics CRM 2013/Fall '13 ユーザー定義アクション その 1

事前準備

操作は関連付く要求/応答クラスのカスタム メッセージを提供
します。まずはこのメッセージ情報を取得する必要があります。

1. Microsoft Dynamics CRM 2013 SDK をダウンロードします。
https://www.microsoft.com/ja-jp/download/details.aspx?id=40321

2. コマンドプロンプトを開き、sdk\bin フォルダに移動します。

3. crmsvcutil.exe を /generateActions パラメータを指定して
実行します。crmsvcutil の使い方はこちらを参照してください。

例) オンライン環境の場合
>crmsvcutil.exe /url:https://<組織名>.api.crm5.dynamics.com/XRMServices/2011/Organization.svc /out:Xrm.cs /username:<ユーザ名> /password:<パスワード> /generateActions

4. 生成された Xrm.cs ファイルを開きます。

5. new_followupLeadRequest および new_followupLeadResponse
が生成されていることを確認します。

6. Visual Studio より C# のコンソールアプリケーションを作成
します。プロジェクト名は CustomActionSample としました。

7. 参照の追加より SDK\bin 配下の Microsoft.Xrm.Sdk.dll を追加
します。また以下参照を追加します。

System.DirectoryServices.AccountManagement
System.Security
System.ServiceModel
System.Runtime.Serialization
Microsoft.IdentityModel.dll

8. 以下のヘルパーコードを追加します。
sdk\samplecode\cs\helpercode\crmservicehelpers.cs
sdk\samplecode\cs\helpercode\deviceidmanager.cs

9. 生成した Xrm.cs を追加します。

10. 一旦プロジェクトをビルドしてエラーがないことを確認します。

コンソールアプリケーションの開発

1. 以下のコードを Program.cs に貼り付けます。簡単なサンプルの
ため、一切のエラーハンドリングはしていません。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Crm.Sdk.Samples;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;

namespace CustomActionSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // ヘルパーを利用してサービスプロキシの生成
            ServerConnection serverConnect = new ServerConnection();
            ServerConnection.Configuration config = serverConnect.GetServerConfiguration();

            using (OrganizationServiceProxy _serviceProxy = ServerConnection.GetOrganizationProxy(config))
            {
                // 事前バインドの利用
                _serviceProxy.EnableProxyTypes();

                // オープンで電話活動がない潜在顧客の取得
                string fetch = @"<fetch version='1.0' output-format='xml-platform'
                     mapping='logical' distinct='false'>
                       <entity name='lead'>
                         <attribute name='fullname' />
                         <attribute name='leadid' />
                          <order attribute='fullname' descending='false' />
                         <link-entity name='phonecall' from='regardingobjectid'
                          to='leadid' alias='ab' link-type='outer'>
                           <attribute name='subject' />
                         </link-entity>
                          <filter type='and'>
                            <condition entityname='lead' attribute='statecode'
                             operator='eq' value='0' />
                            <condition entityname='ab' attribute='regardingobjectid'
                            operator='null' />
                          </filter>
                       </entity>
                      </fetch>";

                EntityCollection results =
                    _serviceProxy.RetrieveMultiple(new FetchExpression(fetch));

                // 取得したレコードに対してカスタムアクションを実行
                foreach (Lead lead in results.Entities)
                {
                    // 操作に紐づく要求の作成
                    new_followupLeadRequest request = new new_followupLeadRequest();
                    // アクションのターゲットとして取得した潜在顧客を指定
                    request.Target = new EntityReference("lead", lead.Id);
                    // 操作に紐づく応答の取得
                    new_followupLeadResponse response =
                         (new_followupLeadResponse)_serviceProxy.Execute(request);
                 }
            }
        }
    }
}

2. プロジェクトをコンパイルして、実行します。

3. 意図したとおり電子メールが作成されれば成功です。

重要な点は、開発者は new_followupLeadRequest/Response が
実際になにを行っているかは知る必要がなく、メッセージ名と
パラメータを理解するだけで良いという事です。

次回はプロセスの変更とトラブルシューティングを紹介します。

- 中村 憲一郎