protected override void RunScenario()

in sdk/SdkSamples/Agreements/GetAllCustomersAgreements.cs [39:95]


        protected override void RunScenario()
        {
            const string noAgreements = "No agreements found.";
            var partnerOperations = this.Context.UserPartnerOperations;

            var csvFilePath = this.ObtainCustomersAgreementCsvFileName();
            File.WriteAllText(csvFilePath, $"TenantId,Domain,Date,First Name,Last Name,Phone,Email {Environment.NewLine}");

            // query the customers, get the first page if a page size was set, otherwise get all customers
            var customersPage = (this.customerPageSize <= 0) ? partnerOperations.Customers.Get() : partnerOperations.Customers.Query(QueryFactory.Instance.BuildIndexedQuery(this.customerPageSize));

            // create a customer enumerator which will aid us in traversing the customer pages
            var customersEnumerator = partnerOperations.Enumerators.Customers.Create(customersPage);

            var count = 0;
            var startTime = DateTime.UtcNow;

            while (customersEnumerator.HasValue)
            {
                foreach (var customer in customersEnumerator.Current.Items)
                {
                    try
                    {
                        // Fetch customer agreements
                        this.Context.ConsoleHelper.WriteObject($"#{++count} Tenant: {customer?.CompanyProfile?.TenantId ?? customer?.Id}, Domain: {customer?.CompanyProfile?.Domain ?? "Domain not available." }", "Customer");
                        var customerAgreements = partnerOperations.Customers.ById(customer.Id).Agreements.ByAgreementType("*").Get();

                        if (!customerAgreements.Items.Any())
                        {
                            this.Context.ConsoleHelper.WriteObject(noAgreements, "Agreement", 1);
                            File.AppendAllText(csvFilePath, $"{customer?.CompanyProfile?.TenantId ?? customer?.Id} ,{customer?.CompanyProfile?.Domain ?? "Domain not available."},,,,,{Environment.NewLine}");
                        }
                        else
                        {
                            // Fetch the latest agreement, if there are multiple instances of the same type
                            var orderedCustomerAgreements = customerAgreements.Items.OrderByDescending(x => x.DateAgreed).ToArray();

                            this.AppendToFile(csvFilePath, customer, orderedCustomerAgreements, "MicrosoftCloudAgreement");
                            this.AppendToFile(csvFilePath, customer, orderedCustomerAgreements, "MicrosoftCustomerAgreement");
                        }
                    }
                    catch (PartnerException partnerException)
                    {
                        if (partnerException.ServiceErrorPayload.ErrorCode.Equals("600009", StringComparison.InvariantCultureIgnoreCase))
                        {
                            this.Context.ConsoleHelper.WriteObject(noAgreements, "Agreement", 1);
                            File.AppendAllText(csvFilePath, $"{customer?.CompanyProfile?.TenantId ?? customer?.Id},{customer?.CompanyProfile?.Domain ?? "Domain not available."},,,,,{Environment.NewLine}");
                        }
                    }
                }

                // get the next page of customers
                customersEnumerator.Next();
            }

            this.Context.ConsoleHelper.WriteObject($"Total Customers: {count} processed in {DateTime.UtcNow - startTime}.");
        }