Hi
I have below code & i want Video Title appears as link . When user clicks it should opne video.
I also want to dsiplay ViewCount
C#
var client = new RestClient("googleapis.com/youtube/v3");
var request = new RestRequest("search", Method.GET);
request.AddParameter("part", "snippet");
request.AddParameter("q", "short videos category");
request.AddParameter("type", "video");
request.AddParameter("maxResults", 25);
request.AddParameter("order", "viewCount");
request.AddParameter("key", "ApiKey");Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 5, 2023, 1:11 AM EST
Hello Ramco,
To display a viewcount you need to get data by each video id. You can use below code for that.
var clientTag = new RestClient("https://www.googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.Get); tagRequest.AddParameter("key", "YOUR_KEY"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("maxResults", 10); tagRequest.AddParameter("id", data.id.videoId); var tagResponse = clientTag.Execute(tagRequest); foreach (var searchResult in tagResponse.Data.items) { Console.WriteLine("Video ID => " + searchResult.id + " Count ==> " + searchResult.statistics.viewCount); }VideoListResponse class is following:
public class VideoListResponse { public string kind { get; set; } public string etag { get; set; } public List items { get; set; } public PageInfo pageInfo { get; set; } public class Item { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public Snippet snippet { get; set; } public Statistics statistics { get; set; } } public class Snippet { public DateTime publishedAt { get; set; } public string channelId { get; set; } public string title { get; set; } public string description { get; set; } public Thumbnails thumbnails { get; set; } public string channelTitle { get; set; } public List tags { get; set; } public string categoryId { get; set; } public string liveBroadcastContent { get; set; } public string defaultLanguage { get; set; } public Localized localized { get; set; } public string defaultAudioLanguage { get; set; } } public class Statistics { public string viewCount { get; set; } public string likeCount { get; set; } public string favoriteCount { get; set; } public string commentCount { get; set; } } public class Thumbnails { public Thumbnail defaultThumb { get; set; } public Thumbnail medium { get; set; } public Thumbnail high { get; set; } public Thumbnail standard { get; set; } public Thumbnail maxres { get; set; } } public class Thumbnail { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class Localized { public string title { get; set; } public string description { get; set; } } public class PageInfo { public int totalResults { get; set; } public int resultsPerPage { get; set; } } }Thanks
Naimish
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 4, 2023, 11:43 PM EST
Hi Nimish
How to write in below line - htmlTable.Append("
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 5, 2023, 12:32 AM EST
Hello Ramoc,
You can write link below:
htmlTable.Append("" + data.snippet.title + "");Thanks
Naimish
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 5, 2023, 12:58 AM EST
Hi Naimish
Secondly how i can display ViewCount value of video
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 4, 2023, 11:45 AM EST
Hello Ramco,
You can create a video link with title as below.
The YoutubeSearchListResponse class is already given in previous answer.
var response = client.Execute(request); foreach (var item in response.Data.items) { var videoLink = "" + item.snippet.title + ""; Console.WriteLine(videoLink); }Thanks
Naimish