示例:XRM Tooling API 快速入门
发布日期: 2017年1月
适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online
QuickStart 示例是一个 Microsoft .NET Framework 托管代码示例显示如何使用 XRM Tooling API 连接到 Microsoft Dynamics 365 示例,并在实体上执行基本创建、更新、检索和删除操作。 有关 XRM Tooling 的更多详细信息,请参阅使用 XRM 工具,构建 Windows 客户端应用程序。
要求
此示例代码适用于 Microsoft Dynamics 365(在线或本地)。 下载 Microsoft Dynamics CRM SDK 包。 它可能位于下载包的以下位置:
SDK\SampleCode\CS\XRMTooling
演示
使用提供常规登录控件内置支持身份验证和凭据缓存和重用的“Dynamics 365 WPF 应用程序” SDK 模板,构建此示例代码。 有关常见登录控件的更多详细信息以及如何使用在 Microsoft Visual Studio 中使用 SDK 模板,请参阅 在您的客户端应用程序中使用 XRM tooling 通用登录控件。
没有用于建立与 Dynamics 365 连接的帮助程序代码。
在连接到 Dynamics 365 后,该示例在客户实体执行基本的创建、更新、检索和删除操作。
当示例第一次运行时,在 c:\Users\<username>\AppData\Roaming\Microsoft\QuickStartXRMToolingWPFClient 文件夹的配置文件 (Default_QuickStartXRMToolingWPFClient.exe.config) 中存储用户凭据,之后指示用户在运行时使用存储的或指定新凭据登录到 Dynamics 365。
如果在生成以下日志文件的过程中出现任何问题,请求助于操作疑难解答:
Login_ErrorLog.log:若要报告登录错误。 此文件位于以下位置 C:\Users\<username>\AppData\Roaming\Microsoft\QuickStartXRMToolingWPFClient。
QuickStartXRMToolingWPFClient.log:若要报告操作错误。 此文件与可执行位于同一位置,即位于 Microsoft Visual Studio 项目的调试文件夹。
若要运行示例
查找并打开 SDK\SampleCode\CS\XRMTooling 文件夹。
在 Visual Studio 中打开 QuickStartXRMToolingWPFClient.sln 文件。
按 F5 编译并运行程序。
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// This namespace is automatically added for using
// components in LoginWindow\CrmLogin.xaml (common login control).
using QuickStartXRMToolingWPFClient.LoginWindow;
// Add this namespace for performing
// various operations in CRM.
using Microsoft.Xrm.Tooling.Connector;
namespace QuickStartXRMToolingWPFClient
{
/// <summary>
/// Demonstrates how to do basic entity operations like create, retrieve,
/// update, and delete using the XRM Tooling APIs and the common login
/// control.</summary>
/// <remarks>
/// At run-time, you will be given the option to delete all the
/// database records created by this program.</remarks>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
btnDelete.Visibility = Visibility.Hidden;
}
#region Class Level Members
private CrmLogin _ctrl = null;
private Guid _accountId;
#endregion Class Level Members
#region How To Sample Code
/// <summary>
/// Connect to Microsoft CRM, and get an instance of CRMServiceClient
/// </summary>
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
// Create an instance of the XRM Tooling common login control
_ctrl = new CrmLogin();
/// Wire event to the CRM sign-in response.
_ctrl.ConnectionToCrmCompleted += ctrl_ConnectionToCrmCompleted;
UpdateStatus("Initiating connection to CRM...");
/// Show the XRM Tooling common login control.
_ctrl.ShowDialog();
/// Validate if you are connected to CRM
if (_ctrl.CrmConnectionMgr != null && _ctrl.CrmConnectionMgr.CrmSvc != null && _ctrl.CrmConnectionMgr.CrmSvc.IsReady)
{
UpdateStatus("Connected to CRM! (Version: " + _ctrl.CrmConnectionMgr.CrmSvc.ConnectedOrgVersion.ToString() +
"; Org: " + _ctrl.CrmConnectionMgr.CrmSvc.ConnectedOrgUniqueName.ToString() + ")");
UpdateStatus("***************************************");
UpdateStatus("Click Start to create, retrieve, update, and delete (optional) an account record.");
btnSignIn.IsEnabled = false;
btnCRUD.IsEnabled = true;
}
// If you are not connected to CRM; display the last error and last CRM excption
else
{
UpdateStatus("The connection to CRM failed or was cancelled by the user.");
}
}
private void btnCRUD_Click(object sender, RoutedEventArgs e)
{
// Create an account record
Dictionary<string, CrmDataTypeWrapper> inData = new Dictionary<string, CrmDataTypeWrapper>();
inData.Add("name", new CrmDataTypeWrapper("Sample Account Name", CrmFieldType.String));
inData.Add("address1_city", new CrmDataTypeWrapper("Redmond", CrmFieldType.String));
inData.Add("telephone1", new CrmDataTypeWrapper("555-0160", CrmFieldType.String));
_accountId = _ctrl.CrmConnectionMgr.CrmSvc.CreateNewRecord("account", inData);
// Validate if the account is created, and then retrieve it
if (_accountId != Guid.Empty)
{
UpdateStatus("***************************************");
UpdateStatus(DateTime.Now.ToString() + ": Created an account with the following" + "\n" + "details, and retrieved it:");
Dictionary<string, object> data = _ctrl.CrmConnectionMgr.CrmSvc.GetEntityDataById("account", _accountId, null);
foreach (var pair in data)
{
if ((pair.Key == "name") || (pair.Key == "address1_city") || (pair.Key == "telephone1"))
{
UpdateStatus(pair.Key.ToUpper() + ": " + pair.Value);
}
}
btnCRUD.IsEnabled = false;
}
else
{
UpdateStatus("***************************************");
UpdateStatus("Could not create the account.");
}
// Update the account record
Dictionary<string, CrmDataTypeWrapper> updateData = new Dictionary<string, CrmDataTypeWrapper>();
updateData.Add("name", new CrmDataTypeWrapper("Updated Sample Account Name", CrmFieldType.String));
updateData.Add("address1_city", new CrmDataTypeWrapper("Boston", CrmFieldType.String));
updateData.Add("telephone1", new CrmDataTypeWrapper("555-0161", CrmFieldType.String));
bool updateAccountStatus = _ctrl.CrmConnectionMgr.CrmSvc.UpdateEntity("account","accountid",_accountId,updateData);
// Validate if the the account record was updated successfully, and then display the updated information
if (updateAccountStatus == true)
{
UpdateStatus("***************************************");
UpdateStatus(DateTime.Now.ToString() + ": Updated the account details as follows:");
Dictionary<string, object> data = _ctrl.CrmConnectionMgr.CrmSvc.GetEntityDataById("account", _accountId, null);
foreach (var pair in data)
{
if ((pair.Key == "name") || (pair.Key == "address1_city") || (pair.Key == "telephone1"))
{
UpdateStatus(pair.Key.ToUpper() + ": " + pair.Value);
}
}
}
else
{
UpdateStatus("***************************************");
UpdateStatus("Could not update the account.");
}
// Prompt the user to delete the account record created in the sample
UpdateStatus("***************************************");
UpdateStatus("To delete the account record created by the sample, click Delete Record.");
UpdateStatus("Otherwise, click Exit to close the sample application.");
btnDelete.Visibility = Visibility.Visible;
}
// Delete the account record created in this sample.
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
btnDelete.IsEnabled = false;
_ctrl.CrmConnectionMgr.CrmSvc.DeleteEntity("account", _accountId);
UpdateStatus("***************************************");
UpdateStatus(DateTime.Now.ToString() + ": Deleted the account record.");
UpdateStatus("Click Exit to close the sample application.");
}
// Exit the sample application
private void btnExit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
/// <summary>
/// Progressively displays the status messages about the actions
/// performed during the running of the sample.
/// <param name="updateText">Indicates the status string that needs to be
/// displayed to the user.</param>
/// </summary>
public void UpdateStatus(string updateText)
{
if (lblStatus.Content.ToString() != String.Empty)
{
lblStatus.Content = lblStatus.Content + "\n" + updateText;
}
else
{
lblStatus.Content = updateText;
}
}
/// <summary>
/// Raised when the login process is completed.
/// </summary>
private void ctrl_ConnectionToCrmCompleted(object sender, EventArgs e)
{
if (sender is CrmLogin)
{
this.Dispatcher.Invoke(() =>
{
((CrmLogin)sender).Close();
});
}
}
}
#endregion How To Sample Code
}
另请参阅
在您的客户端应用程序中使用 XRM tooling 通用登录控件
使用 XRM 工具,构建 Windows 客户端应用程序
了解 Microsoft Dynamics 365 开发的教程和资源
Microsoft Dynamics 365
© 2017 Microsoft。 保留所有权利。 版权