How to add source parameter in searching on newsapi in C#?

I am trying to search on newsapi.org a result from specific news sources. How could I achieve it?

I have the following code on C#, I did search on its documentation but there has no example that requires a specific source.

Sources in EverythingRequest is a list. public List Sources;

public class EverythingRequest{
        //     If you want to restrict the search to specific sources, add their Ids here. You
        //     can find source Ids with the /sources endpoint or on newsapi.org.
        public List<string> Sources;
        public List<string> Domains;

        public EverythingRequest();
        public string Q { get; set; }
        public DateTime? From { get; set; }
        public DateTime? To { get; set; }
        public Languages? Language { get; set; }
        public SortBys? SortBy { get; set; }
        public int Page { get; set; }
        public int PageSize { get; set; }
}

private async Task RetrieveItems()
        {
var newsApiClient = new NewsApiClient("myAPIKEY");            
            ArticlesResult articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest
            {
                Q = "skin cancer risk",
                SortBy = SortBys.Popularity,
                Language = Languages.EN,
                PageSize = 10,
                Sources = "cnn",
            });
}

Error CS0029 Cannot implicitly convert type 'string' to 'System.Collections.Generic.List'

1 answer

  • answered 2019-10-08 03:32 reno roy sorima

    Thank you to -Chetan Ranpariya, it works..

    modified code is here..

    ArticlesResult articlesResponse = await newsApiClient.GetEverythingAsync(new EverythingRequest

    {

    Q = "skin cancer risk",

    SortBy = SortBys.Popularity,

    Language = Languages.EN,

    PageSize = 10,

    Sources = new List { "cnn" },});

    you can more value to sources, depending on the source at https://newsapi.org/sources.