SDK 4.0.12 の xRM を使ってみよう パート 3
みなさん、こんにちは。
昨日ブログのスキンを変更してみました。見やすくなったでしょうか?是非ご意見を。
今回はちょっと趣向を変えて、WPF での開発を紹介したいと思います。
せっかくなので Visual Studio 2010 を使ってみましたが、時間と環境の都合で英語版の
UI になってしまったことを事前にお詫びします。
前提
- Dynamics CRM 4.0 UR11
- SDK 4.0.12
- Visual Studio 2010
- 関連記事パート1 で紹介した Xrm.cs
では早速開発してみましょう。
下準備
1. Visual Studio 2010 を起動して、新しいソリューションを作成します。
今回は Visual C# –> Windows –> WPF Application を選択します。また .NET は 3.5 です。
ソリューションの名前は Xrm.AccountDetails としました。
2. 参照設定で以下を追加。
ファイル:
sdk\bin\microsoft.crm.sdk.dll
sdk\bin\microsoft.crm.sdktypeproxy.dll
sdk\bin\microsoft.crm.sdktypeproxy.xmlserializers.dll
sdk\microsoft.xrm\bin\microsoft.xrm.client.dll
sdk\microsoft.xrm\bin\microsoft.xrm.portal.dll
sdk\microsoft.xrm\bin\microsoft.xrm.portal.files.dll
.NET
System.Data.Entity
System.Data.Services
System.Data.Services.Client
またツールで生成した Xrm.cs も既存の項目より追加します。これで準備OKです。
3. アプリケーション構成ファイルを追加します。内容は パート1 と同じです。
<configuration>
<connectionStrings>
<add name="OnPremise" connectionString="Authentication Type=Integrated;
Server=https://crm:5555/AdventureWorks" />
</connectionStrings>
</configuration>
コレクションの作成
今回のプログラムで使用するコレクションを定義します。
1. 新規項目の追加より、コードファイルを追加してください。名前は CrmCollections.cs です。
2. 以下のソースを貼り付けてください。
public class AccountCollection : System.Collections.ObjectModel.ObservableCollection<Xrm.account>
{
public AccountCollection()
{
}
}
public class SalesOrderCollection : System.Collections.ObjectModel.ObservableCollection<Xrm.salesorder>
{
public SalesOrderCollection()
{
}
public SalesOrderCollection(Guid customerId, Xrm.ADVDataContext context)
{
Microsoft.Crm.Sdk.Customer customer = new Microsoft.Crm.Sdk.Customer("account", customerId);
context.salesorders.Where(order => order.customerid.Value == customer.Value).ToList().ForEach(order => Add(order));
}
}
public class SalesOrderDetailCollection : System.Collections.ObjectModel.ObservableCollection<Xrm.salesorderdetail>
{
public SalesOrderDetailCollection()
{
}
public SalesOrderDetailCollection(Guid salesOrderId, Xrm.ADVDataContext context)
{
Xrm.salesorderdetail detail = new Xrm.salesorderdetail();
context.salesorderdetails.Where(sod => sod.salesorderid.Value == salesOrderId).ToList().ForEach(sod => Add(sod));
}
}
これで 3 つのカスタムコレクションが作成できました。
画面の作成
では次に画面を作っていきます。完成イメージは以下のとおりです。
表記は英語にしていますが、自由に変更してください。 以下 XAML です。
<Window x:Class="Xrm.AccountDetails.MainWindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xrm="clr-namespace:Xrm.AccountDetails"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<xrm:AccountCollection x:Key="AccountListDataSource"/>
<xrm:SalesOrderCollection x:Key="SalesOrderListDataSource"/>
<xrm:SalesOrderDetailCollection x:Key="SalesOrderDetailListDataSource"/>
</Window.Resources>
<Grid Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid Name="SearchGrid" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="GetAccountsButton" Click="GetAccountsButton_Click">Get Accounts</Button>
</Grid>
<Grid Name="AccountsGrid" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Label Grid.Row="0">Accounts</Label>
<ListView Grid.Row="1" Name="AccountsGridListView" ItemsSource="{Binding Source={StaticResource ResourceKey=AccountListDataSource}}" SelectionChanged="AccountsGridListView_SelectionChanged">
<ListView.View>
<GridView x:Name="AccountsGridListViewGrid">
<GridViewColumn Header="Account Name" DisplayMemberBinding="{Binding Path=name}"/>
<GridViewColumn Header="Primary Contact Owner" DisplayMemberBinding="{Binding Path=account_primary_contact.contact_owning_user.fullname}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
<Grid Name="OrdersGrid" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Label Grid.Row="0">Sale Orders</Label>
<ListView Grid.Row="1" Name="OrdersGridListView" ItemsSource="{Binding Source={StaticResource ResourceKey=SalesOrderListDataSource}}" SelectionChanged="OrdersGridListView_SelectionChanged">
<ListView.View>
<GridView x:Name="OrdersGridListViewGrid">
<GridViewColumn Header="Sales Order Number" DisplayMemberBinding="{Binding Path=ordernumber}"/>
<GridViewColumn Header="Total Order Amount" DisplayMemberBinding="{Binding Path=totalamount}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
<Grid Name="OrderDetailsGrid" Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Label Grid.Row="0">Sale Order Details</Label>
<ListView Grid.Row="1" Name="OrderDetailsGridListView" ItemsSource="{Binding Source={StaticResource ResourceKey=SalesOrderDetailListDataSource}}">
<ListView.View>
<GridView x:Name="OrderDetailsGridLiveViewGrid">
<GridViewColumn Header="Product Name" DisplayMemberBinding="{Binding Path=product_order_details.name}"/>
<GridViewColumn Header="Quantity" DisplayMemberBinding="{Binding Path=quantity}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Grid>
</Window>
コードの作成
では、xaml の裏のコードを書いてみましょう。コードにコメントを入れておきましたので、参考にしてください。
// サービス用のコンテキストの作成
private Xrm.ADVDataContext _context;
public MainWindow()
{
InitializeComponent();
// サービスの作成
_context = new Xrm.ADVDataContext("OnPremise");
}
// 取引先担当者の結果を選択した場合の処理 - 関連した発注を取得
private void AccountsGridListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Xrm.account selectedAccount = AccountsGridListView.SelectedItem as Xrm.account;
if (selectedAccount != null)
{
OrdersGridListView.ItemsSource = new SalesOrderCollection(selectedAccount.accountid, _context);
}
OrderDetailsGridListView.ItemsSource = new SalesOrderDetailCollection();
}
// 発注の結果を選択した場合の処理 - 関連した詳細を取得
private void OrdersGridListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Xrm.salesorder selectedOrder = OrdersGridListView.SelectedItem as Xrm.salesorder;
if (selectedOrder != null)
{
OrderDetailsGridListView.ItemsSource = new SalesOrderDetailCollection(selectedOrder.salesorderid, _context);
}
}
// 取引先担当者の取得
private void GetAccountsButton_Click(object sender, RoutedEventArgs e)
{
AccountCollection accounts = new AccountCollection();
_context.accounts.OrderBy(acct => acct.name).ToList().ForEach(acct => accounts.Add(acct));
AccountsGridListView.ItemsSource = accounts;
}
実行してテスト
最後は実行するだけです。うまく動いたでしょうか?
ご覧のように、コレクションを作っておき、あとはItemsSource に結果を指定するだけで簡単に結果を
出力できます。ソートなども合わせてたった 1 行ですべてを記述することができるのは感動ですね!
WPF を使えば簡単に Windows クライアントも表現できますので是非チャレンジしてください。
参考URL
Windows Presentation Foundation
https://www.microsoft.com/products/expression/ja/wpf/default.mspx
- Dynamics CRM サポート 中村 憲一郎