範例: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 所提供範例程式碼的需求資訊,請參閱使用範例和 Helper 程式碼

示範

Windows 8 範例應用程式主畫面

範例應用程式的圖標並排使用者介面

此範例顯示如何撰寫 Windows 8.1 桌面現代應用程式,可以傳送要求至組織 Web 服務,且不用連結 SDK 組件。 此範例使用 Microsoft Azure Active Directory Authentication Library (ADAL) 和 SOAP 通訊協定。 範例也示範如何在執行階段取得 OAuth 端點 URL。

雖然在主要應用程式頁面上顯示七個圖標,但是只有客戶與工作圖標已連接至事件處理常式程式碼。 其他圖標是預留位置。

範例程式碼是針對 Microsoft Dynamics CRM Online 伺服器和虛擬組織進行設定的,但也適用於 IFD 伺服器。

顯示完整範例的主要區段的程式碼片段在本主題後面顯示。

範例

下列程式碼片段會顯示如何使用組織 Web 服務驗證使用者。

若要執行此程式碼,必須先使用支援的身分識別提供者 (AD FS 或 Microsoft Azure Active Directory) 註冊應用程式。 接下來,您必須在程式碼中設定 _clientID 和 CrmServiceUrl 的值。 用戶端識別碼的值在應用程式註冊時定義。 如需有關應用程式註冊的詳細資訊,請參閱 逐步解說:向 Active Directory 註冊 CRM 應用程式

範例

下列程式碼片段會顯示如何在 HTTP 要求中使用 SOAP 程式碼,從組織 Web 服務擷取實體記錄。 驗證存取 token 放置在授權標題中。


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 工具的相關資訊,請參閱 逐步解說:使用現代應用程式 SOAP 端點與 JavaScript

另請參閱

建立新型行動應用程式
使用 Web 服務驗證使用者
範例:Windows 8 桌面現代 OData 應用程式
Windows 市集的 Azure 驗證程式庫 (AAL):深入了解分析
使用 Azure AD 保護 Windows 市集應用程式和 REST Web 服務 (預覽)
了解 SOAP

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權