C# retrieve calllogs example using client api

  • 1
  • Idea
  • Updated 3 months ago
// Hope this helps someone I know I struggled with using the api initially.

// Import the Ring Central Client api into VS using nuget first.

using System;
using System.Linq;
using RingCentral;

namespace ringCentralLogs
{
    class Program
    {
        static void Main(string[] args)
        {
            Conn();
            Console.ReadLine();
        }

        static async void Conn()
        {
            try
            {

                RestClient rc = new RestClient("ClientId", "ClientSecret");

                // Authorize the user.
                await rc.Authorize("Sandbox User ID", "", "Password");
 
               // Get the main account.
                var account = rc.Restapi().Account();

               // Retrieve the call log records using some selection criteria.
                var callLogs = await account.CallLog().List(new { direction = "Outbound", type = "Fax", dateFrom = "2018-03-10T18:07:52.534Z"});

                Console.WriteLine("Print the call log");

                // Convert the callLogs.records to a list.
                var query = callLogs.records.ToList();

                // Iterate over the list printing properties.
                foreach (var result in query)
                {
                    Console.WriteLine("StartTime: {0}  To: {1}  Direction: {2}  Type: {3}  Result: {4}",result.startTime,result.to.phoneNumber, result.direction, result.type, result.result);
                }

                // Revoke the token.
                await rc.Revoke();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return;
        }
    }
}

Photo of Alan LaPenn

Alan LaPenn

  • 100 Points 100 badge 2x thumb

Posted 4 months ago

  • 1
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,492 Points 5k badge 2x thumb
What is the issue? Did you encounter any errors? Could you please post more detail?
Photo of Alan LaPenn

Alan LaPenn

  • 100 Points 100 badge 2x thumb
No Issue at all it was working fine. However when calling async you must use thread.sleep or the app exits before the retrieval of the logs completes.
(Edited)
Photo of Tyler Long

Tyler Long, Official Rep

  • 6,858 Points 5k badge 2x thumb
It is the expected behavior. Because the SDK is async. It won't block or stop your app from quitting.

You can add Console.ReadLine(); at the end to prevent it from quitting.
Photo of David Southern

David Southern

  • 260 Points 250 badge 2x thumb
One question I have is this:  Is the array of objects returned from the call log a strongly typed list?  In other words, in this line here:

 var query = callLogs.records.ToList();

Is query a list<callLogRecord> or something of that nature?
Photo of Tyler Long

Tyler Long, Official Rep

  • 7,822 Points 5k badge 2x thumb
Here is the code of that method: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral/Paths/CallLogPath.c...

You can see the the returned value is of type ExtensionCallLogResponse
Photo of Alan LaPenn

Alan LaPenn

  • 100 Points 100 badge 2x thumb
Yes, I it is a list of call log records and I iterate over the list using foreach (var result in query), where result is a single call log record. The properties of which are accessed using  dot notation., from my example code I use result.startTime to get the start time from the log record. all of the properties can be accessed this way. There are call log properties which are subordinate so the must be accessed by further dot notation like result.to.phoneNumber and result.to.name.

Hope this helps.
Photo of David Southern

David Southern

  • 260 Points 250 badge 2x thumb
That sounds nice.  I was afraid the data would come back in raw json and I'd have to build poco classes to deserialize it to.  

Thanks!
Photo of John Wang

John Wang, Official Rep

  • 5,622 Points 5k badge 2x thumb
This SDK has model classes that are auto-generated from our OpenAPI / Swagger spec. It is built using "rc-codegen" project here:

https://github.com/tylerlong/rc-codegen
Photo of Tyler Long

Tyler Long, Official Rep

  • 7,822 Points 5k badge 2x thumb
You don't have to deserialize it yourself. There are predefined classes: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral/Definitions/Extensi...