Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
When making a list by query request in GMail via C#, what's the "source"?

Context: C#, JavaScript, ClearScript

I've written a plugin for my ClearScript-enabled JavaScript that connects to GMail. Everything's fine regarding OAuth2 etc. What's not working is a query against the inbox. If I request the inbox without a query, that gives me data, but I don't want millions of records.

attach(".\\Plugin_GMail.dll")
var tuple = Plugin_GMail.GoogleMail.Mail.Mail_Authenticate(REDACTED)
var service = Plugin_GMail.GoogleMail.Mail.Mail_CreateService(tuple.Item1, "mail")
var request = service.Users.Labels.List("me");
var labels = request.Execute().Labels;
var before = new Date(2022,11,31).valueOf();
var after = new Date(2022,0,1).valueOf();
var res = Plugin_GMail.GoogleMail.Mail.Messages_ListByQuery(service, "me", "before:$b after:$a".replace("$b",before).replace("$a",after),true)

The code for Messages_ListByQuery is

        public static string Messages_ListByQuery(GmailService service, string userId, string query, bool debug = false)
        {
            if (debug) Debugger.Launch();
            var msgList = new List<Message>();

            var result = new JSONResponse();

            var request = service.Users.Messages.List(userId);
            request.MaxResults = 500;
            request.Q = query;
            request.LabelIds = "INBOX";

            while (true)
            {
                try
                {                   
                    var msgs = request.Execute().Messages;
                    if (msgs.Count() == 0)
                    {
                        break;
                    }
                    msgList.AddRange(msgs);
                }
                catch (Exception e)
                {
                    result.Error = e.Message;
                    break;
                }
            }
            result.Cargo = msgList;
            return JsonConvert.SerializeObject(result);
        }

So the response after executing the query-based List is

{"Error":"Value cannot be null.\r\nParameter name: source","Cargo":[],"Crew":null}

I have been through the documentation and can't find anything about "source". And there's nothing in the Playground about "source" either. Where next?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code is telling it to just loop forever, getting the same page of results.

while (true)   // loop forever
        {
            try
            {                   
                var msgs = request.Execute().Messages;  // get first page of results
                if (msgs.Count() == 0)  
                {
                    break; // will never hit unless user has no messages. in their inbox at all.
                }
                msgList.AddRange(msgs);  // keeps adding first page of messages over and over.
            }
            catch (Exception e)
            {
                result.Error = e.Message;
                break;
            }
        }

The line

var msgs = request.Execute().Messages;

Is just going to say run the request and get me messages. Which will always return messages as your running the same request over and over.

The following line is just going to add the messages to your list.

msgList.AddRange(msgs);

So with the while true you are telling your app to just request the same 500 rows again and again forever.

You should instead look at using the next page token and getting the next page of results instead of the same results over and over.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!