示例:Windows 8 桌面现代 SOAP 应用程序

 

发布日期: 2016年11月

适用于: Dynamics CRM 2015

此示例代码适用于 Microsoft Dynamics CRM 2015 和 Microsoft Dynamics CRM Online 2015 更新。 它可能位于下载包的以下位置:

SampleCode\CS\ModernAndMobileApps\ModernSoapApp

下载 Microsoft Dynamics CRM SDK 包。

要求

此示例要求 Microsoft.Preview.WindowsAzure.ActiveDirectory.AuthenticationMicrosoft.IdentityModel.Clients.ActiveDirectory NuGet 包。 如果您有可用的网络连接,则在下载项目解决方案时,可以自动下载和安装这些程序包。

有关运行此 SDK 中提供的示例代码的要求的详细信息,请参阅使用示例和帮助程序代码

演示

Windows 8 示例应用程序主屏幕

示例应用程序的平铺用户界面

此示例说明了在不链接到 SDK 程序集的情况下,如何编写可以向组织 Web 服务发送请求的 Windows 8.1 桌面现代应用程序。 此示例使用的是 Microsoft Azure Active Directory 身份验证库 (ADAL) 和 SOAP 协议: 此示例还演示在运行时,获取 OAuth 终结点 URL 。

尽管主应用程序页面显示了七个图块,只有账户和任务图块连接到事件处理程序代码。 其他图块仅仅是占位符。

示例代码针对 Microsoft Dynamics CRM Online 服务器和虚拟组织配置,但也用于 IFD 服务器。

完整示例的主要部分的代码段将在本主题的后面部分显示。

示例

以下代码段显示的是如何使用组织 Web 服务对用户进行验证。

若要运行此代码,您必须首先使用支持的身份提供商(AD FS 或 Microsoft Azure Active Directory)注册应用程序。 然后,您必须在代码中为 _clientID 和 CrmServiceUrl 设置变量值。 客户端的 ID 值是在应用程序注册期间定义的。 有关应用程序注册的详细信息,请参阅 演练:使用 Active Directory 注册 CRM 应用程序

示例

下列代码段说明了如何在 HTTP 请求中使用 SOAP 协议代码检索组织 Web 服务中的实体记录。 身份验证访问令牌在授权标题中。


using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace ModernSoapApp
{
    public static class HttpRequestBuilder
    {
        /// <summary>
        /// Retrieve entity record data from the organization web service. 
        /// </summary>
        /// <param name="accessToken">The web service authentication access token.</param>
        /// <param name="Columns">The entity attributes to retrieve.</param>
        /// <param name="entity">The target entity for which the data should be retreived.</param>
        /// <returns>Response from the web service.</returns>
        /// <remarks>Builds a SOAP HTTP request using passed parameters and sends the request to the server.</remarks>
        public static async Task<string> RetrieveMultiple(string accessToken, string[] Columns, string entity)
        {
            // Build a list of entity attributes to retrieve as a string.
            string columnsSet = string.Empty;
            foreach (string Column in Columns)
            {
                columnsSet += "<b:string>" + Column + "</b:string>";
            }

            // Default SOAP envelope string. This XML code was obtained using the SOAPLogger tool.
            string xmlSOAP =
             @"<s:Envelope xmlns:s='https://schemas.xmlsoap.org/soap/envelope/'>
                <s:Body>
                  <RetrieveMultiple xmlns='https://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
                    <query i:type='a:QueryExpression' xmlns:a='https://schemas.microsoft.com/xrm/2011/Contracts'><a:ColumnSet>
                    <a:AllColumns>false</a:AllColumns><a:Columns xmlns:b='https://schemas.microsoft.com/2003/10/Serialization/Arrays'>" + columnsSet +
                   @"</a:Columns></a:ColumnSet><a:Criteria><a:Conditions /><a:FilterOperator>And</a:FilterOperator><a:Filters /></a:Criteria>
                    <a:Distinct>false</a:Distinct><a:EntityName>" + entity + @"</a:EntityName><a:LinkEntities /><a:Orders />
                    <a:PageInfo><a:Count>0</a:Count><a:PageNumber>0</a:PageNumber><a:PagingCookie i:nil='true' />
                    <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
                    </a:PageInfo><a:NoLock>false</a:NoLock></query>
                  </RetrieveMultiple>
                </s:Body>
              </s:Envelope>";

            // The URL for the SOAP endpoint of the organization web service.
            string url = CurrentEnvironment.CrmServiceUrl + "/XRMServices/2011/Organization.svc/web";

            // Use the RetrieveMultiple CRM message as the SOAP action.
            string SOAPAction = "https://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple";

            // Create a new HTTP request.
            HttpClient httpClient = new HttpClient();

            // Set the HTTP authorization header using the access token.
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            // Finish setting up the HTTP request.
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url);
            req.Headers.Add("SOAPAction", SOAPAction);
            req.Method = HttpMethod.Post;
            req.Content = new StringContent(xmlSOAP);
            req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");

            // Send the request asychronously and wait for the response.
            HttpResponseMessage response;
            response = await httpClient.SendAsync(req);
            var responseBodyAsText = await response.Content.ReadAsStringAsync();

            return responseBodyAsText;
        }
    }
}

您可以在 SDK SOAPLogger 下载中使用该工具提供程序获取 SOAP 组织请求中的代码。 有关使用 SOAPLogger 工具的详细信息,请参阅 演练:与 JavaScript 结合使用现代应用程序 SOAP 终结点

另请参阅

编写移动和现代应用程序
通过 Web 服务对用户进行验证
示例:Windows 8 桌面现代 OData 应用程序
用于 Windows 应用商店的 Azure 身份验证库 (AAL):深入研究
使用 Azure AD 获取 Windows 应用商店应用程序和 REST Web 服务(预览)
了解 SOAP

© 2017 Microsoft。 保留所有权利。 版权