Пример #1
0
        private HttpClient httpClient;      // Client to CRM server communication

        /// <summary>
        /// Primary method that demonstrates the Data Export Service API.
        /// </summary>
        public async Task RunAsync()
        {
            //HttpRequestMessage request;
            //HttpResponseMessage response;

            string myOrgUrl = "https://crmue.crm.dynamics.com/";      //Dynamics 365 organization URL
            string myOrgId  = "883278f5-07af-45eb-a0bc-3fea67caa544"; //Dynamics 365 organization ID

            //Call a method just to confirm connection
            var response = httpClient.GetAsync("WhoAmI",
                                               HttpCompletionOption.ResponseHeadersRead).Result;

            if (response.IsSuccessStatusCode)
            {
                //Get the response content and parse it.
                JObject body   = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                Guid    userId = (Guid)body["UserId"];
                Console.WriteLine("Your system user ID is: {0}", userId);
            }
            else
            {
                Console.WriteLine("The WhoAmI request failed with a status of '{0}'",
                                  response.ReasonPhrase);
            }


            //Use the metadata API to obtain a connector instance to your Dynamics 365 service.
            DataExportSalesClient   discoveryClient = new DataExportSalesClient();
            ConnectorDetailResponse connResponse    = (ConnectorDetailResponse)discoveryClient.
                                                      Metadata.GetConnectorDetailsAsync(myOrgUrl, myOrgId).Result;

            if (connResponse == null)
            {
                Console.Write("Failed to obtain a connector instance from the discovery service!");
                throw new NullReferenceException("ConnectorDetailResponse is null!");
            }

            //Use the connector to perform profile operations via the profiles API.
            //First, create another client instance, but now using the discovered connector URL.
            DataExportSalesClient salesClient =
                new DataExportSalesClient(new Uri(connResponse.ConnectorUrl));
            //Then obtain the collection of sales profiles.
            Profiles salesProfiles = new Profiles(salesClient);

            //With this client, can work with an existing Data Export profile.
            salesProfiles.Activate("SalesExport1");
            ProfileDetailsDTO salesProfile1 = (ProfileDetailsDTO)salesProfiles.GetProfileById("SalesExport1");


            //Or can create new Data Export profile.
            ProfileDetailsDTO salesProfile2 =
                (ProfileDetailsDTO)salesProfiles.CreateProfile(new DataExportSales.Models.ProfileDescriptionBase()
            {
                DestinationKeyVaultUri = "https://mykv.vault.azure.net:443/secrets/SampleDataExportSecret/f593bcd8f3b8461584935e0a3e7325dd;",
                Name = "SalesExport1"
                       //...
            });



            //Test the new profile.
            object testResult = salesProfiles.GetTestResultById(salesProfile1.Id);

            //Activate the new profile.
            salesProfiles.Activate(salesProfile1.Id);
        }