Question

Message Store Export C# Index does not contain definition for List

  • 29 February 2024
  • 2 replies
  • 108 views

I am trying to test out the Message Store Export (kinda new at this) and am running into some errors. I am using the code from the documentation here. I already replaced the depreciated WebClient with HTTP but am still getting this error:

Error CS1061 'Index' does not contain a definition for 'List' and no accessible extension method 'List' accepting a first argument of type 'Index' could be found (are you missing a using directive or an assembly reference?)

I do have the RingCentral SDK installed. Any help would be appreciated!

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using RingCentral;

namespace Export_MessageStore
{
    class Program
    {
        static RestClient restClient;
        static HttpClient httpClient = new HttpClient();

        static async Task Main(string[] args)
        {
            try
            {
                // Instantiate the SDK
                restClient = new RestClient("SANDBOX-APP-CLIENTID", "SANDBOX-APP-CLIENTSECRET", "https://platform.devtest.ringcentral.com");
                // Authenticate a user using a personal JWT token
                await restClient.Authorize("SANDBOX_JWT");

                await CreateMessageStoreReport();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static private async Task CreateMessageStoreReport()
        {
            var bodyParams = new CreateMessageStoreReportRequest
            {
                dateFrom = "2023-03-01T00:00:00.000Z",
                dateTo = "2023-03-31T23:59:59.999Z"
            };

            var response = await restClient.Restapi().Account().MessageStoreReport().Post(bodyParams);
            await GetMessageStoreReportTask(response.id);
        }

        static private async Task GetMessageStoreReportTask(string taskId)
        {
            bool isCompleted = false;
            while (!isCompleted)
            {
                var response = await restClient.Restapi().Account().MessageStoreReport(taskId).Get();
                Console.WriteLine("Task creation status: " + response.status);
                switch (response.status)
                {
                    case "Completed":
                        isCompleted = true;
                        await GetMessageStoreReportArchive(taskId);
                        break;
                    case "AttemptFailed":
                    case "Failed":
                    case "Cancelled":
                        Console.WriteLine("Export message store failed.");
                        return;
                    default:
                        await Task.Delay(10000); // Use non-blocking delay
                        break;
                }
            }
        }

        static private async Task GetMessageStoreReportArchive(string taskId)
        {
            Console.WriteLine("Getting report uri ...");
            var resp = await restClient.Restapi().Account().MessageStoreReport(taskId).Archive().List();
            var dateStr = DateTime.Now.ToString("yyyy-MM-dd-HH_mm");
            for (var i = 0; i < resp.records.Length; i++)
            {
                var fileName = $"message_store_content_{dateStr}_{i}.zip";
                var contentUrl = $"{resp.records[i].uri}?access_token={restClient.token.access_token}";

                // Use HttpClient for downloading the file
                var response = await httpClient.GetAsync(contentUrl);
                if (response.IsSuccessStatusCode)
                {
                    using (var stream = await response.Content.ReadAsStreamAsync())
                    using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                    {
                        await stream.CopyToAsync(fileStream);
                    }
                    Console.WriteLine($"{fileName} file is saved to the local machine.");
                }
                else
                {
                    Console.WriteLine($"Failed to download file: {fileName}");
                }
            }
        }
    }
}

2 replies

Userlevel 1

There was a change and it causes the mistake in the sample code. Please modify the code in this function. Change List() to Get().

static private async Task get_message_store_report_archive(String taskId)
{
    Console.WriteLine("Getting report uri ...");
    var resp = await restClient.Restapi().Account().MessageStoreReport(taskId).Archive().Get();
    DateTime value = DateTime.Now;
    ...
}

I will update the sample code soon.

For some reason when I filter to SMS, I get nothing. I can Read_MessageStore and filter it to SMS and that come out okay. When I filter this line of code (with your awesome fix), nothing comes up when it is outputted. Is filtering not able to be used when exporting?

        static private async Task CreateMessageStoreReport()
        {
            var bodyParams = new CreateMessageStoreReportRequest
            {
                dateFrom = "2024-03-01T12:00:00.000Z",
                dateTo = "2024-03-01T13:00:00.000Z",
                messageTypes = new[] {"SMS"}
            };

            var response = await restClient.Restapi().Account().MessageStoreReport().Post(bodyParams);
            await GetMessageStoreReportTask(response.id);
        }

        static private async Task GetMessageStoreReportTask(string taskId)
        {
            bool isCompleted = false;
            while (!isCompleted)
            {
                var response = await restClient.Restapi().Account().MessageStoreReport(taskId).Get();
                Console.WriteLine("Task creation status: " + response.status);
                switch (response.status)
                {
                    case "Completed":
                        isCompleted = true;
                        await GetMessageStoreReportArchive(taskId);
                        break;
                    case "AttemptFailed":
                    case "Failed":
                    case "Cancelled":
                        Console.WriteLine("Export message store failed.");
                        return;
                    default:
                        await Task.Delay(10000); // Use non-blocking delay
                        break;
                }
            }
        }

        static private async Task GetMessageStoreReportArchive(string taskId)
        {
            Console.WriteLine("Getting report uri ...");
            var resp = await restClient.Restapi().Account().MessageStoreReport(taskId).Archive().Get();
            var dateStr = DateTime.Now.ToString("yyyy-MM-dd-HH_mm");
            for (var i = 0; i < resp.records.Length; i++)
            {
                var fileName = $"message_store_content_{dateStr}_{i}.zip";
                var contentUrl = $"{resp.records[i].uri}?access_token={restClient.token.access_token}"

Reply