Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

how to make asychronous method calls return a List ?

Hi,

I am using google api to search for youtube videos but unfortunately I can not really resolve that altough i search for the video and get a result in the function i can not return a value from the function thus wont be able to display this on my page.

Ideally what i would want to do is reeturn a List from the function and iterate through each item on the List in razor and display the video but how can i achieve this can you help me?

 public partial class YouTubeConnect : System.Web.UI.Page
    {

        List<string> videos = new List<string>();
        List<string> channels = new List<string>();
        List<string> playlists = new List<string>();
      
        protected void Page_Load(object sender, EventArgs e)
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = MazdaContext.Current.Site.GetConfigurationValue("youtubeApiKey"),
                ApplicationName = this.GetType().ToString()
            });
           
            SearchVideo(youtubeService);

        }

        public async void SearchVideo(YouTubeService youtubeService)
        {
            var searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.Q = "BMW"; //Replace with your search term.
            searchListRequest.MaxResults = 50;

            // Call the search.list method to retrieve results matching the specified query term.
            var searchListResponse = await searchListRequest.ExecuteAsync();

            // Add each result to the appropriate list, and then display the lists of
            // matching videos, channels, and playlists.
            foreach (var searchResult in searchListResponse.Items)
            {
                switch (searchResult.Id.Kind)
                {
                    case "youtube#video":
                        videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                        break;

                    case "youtube#channel":
                        channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                        break;

                    case "youtube#playlist":
                        playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                        break;

                       
                }
            }
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial