I have the following functions to get messages using Graph API
var client = new GraphServiceClient(authenticationProvider);
var messages = await client.Users["useer#domain.com"].Messages
.Request()
.GetAsync();
I am only able to get the latest 10 messages. How do I get all the messages? I tried to have a look at the microsoft documentation here: https://learn.microsoft.com/en-us/graph/api/message-get?view=graph-rest-1.0&tabs=csharp but unable to find any clues.
Found the answer after googling and trial error.
IUserMessagesCollectionPage msgs = await _client.Users[user#domain.com].Messages.Request()
.Filter("put your filter here")
.GetAsync();
List<Message> messages = new List<Message>();
messages.AddRange(msgs.CurrentPage);
while (msgs.NextPageRequest != null)
{
msgs = await msgs.NextPageRequest.GetAsync();
messages.AddRange(msgs.CurrentPage);
}
You can do it with .Top():
var client = new GraphServiceClient(authenticationProvider);
var messages = await client.Users["user#domain.com"].Messages
.Request()
.Top(100)
.GetAsync();
I think you should refer to this document:
Depending on the page size and mailbox data, getting messages from a mailbox can incur multiple requests. The default page size is 10 messages. To get the next page of messages, simply apply the entire URL returned in #odata.nextLink to the next get-messages request. This URL includes any query parameters you may have specified in the initial request.
Related
I'm trying to download multiple emails using a single Microsoft Graph SDK call.
This is the code I'm using:
foreach (var emailId in emailIds)
{
var request = graphServiceClient.Users[EmailAddress].Messages[emailId].Request();
request.Headers.Add(new HeaderOption("Accept", "message/rfc822"));
batchRequestContent.AddBatchRequestStep(request);
}
var result = await graphServiceClient.Batch.Request().PostAsync(batchRequestContent, cancellationToken);
This call results in the exception:
Microsoft.Graph.ServiceException: 'Code: BadRequest
Message: Invalid batch payload format.
What do I need to change to get this to work, or is it simply not possible?
I followed this page to setup a MicrosoftGraphProvider: http://www.keithmsmith.com/get-started-microsoft-graph-api-calls-net-core-3/
This is working correctly, as I am able to get a list of all of my users with the following request.
var user = await _graphServiceClient.Users.Request().GetAsync();
However, I don't always want all of the users returned, so I have a filter on a user by email.
The example says to do this
var user = await _graphServiceClient.Users[email].Request().GetAsync();
But this always results in user not found, even if I pass a valid email from the response of all users.
So I tried to build a filter, and do it this way.
var test = await _graphServiceClient.Users["$filter=startswith(mail,'test#email.com')"].Request().GetAsync();
var test = await _graphServiceClient.Users["$filter=(startswith(mail,'test#email.com'))"].Request().GetAsync();
Both of these returned the error:
Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: BadRequest
Message: The $filter path segment must be in the form $filter(expression), where the expression resolves to a boolean.
This filter works fine when I use it in Postman calling the url directly. But I am trying to use their sdk and it is not working as expected.
What is wrong with this filter query?
$filter should be specified in Filter method. The article you followed does not reflect the current API.
var users = await _graphServiceClient.Users
.Request()
.Filter("startswith(mail,'test#email.com')")
.GetAsync();
Check documentation
I want to get an email from my gmail sent items via c#.
I used this
service.Users.Messages.Get("me",id);
but it get 404 error.
All other apis works properly.
Thanks.
404 means that the Id you are requesting does not exist. I would run a List first then a get after.
If you want to see messages that are in the sent folder you should do a message.list
and search for what is in the sent folder.
var request = service.Users.Messages.List("me");
request.Q = "is:sent";
var result = request.Execute();
If you know when it was sent you could add a date.
var request = service.Users.Messages.List("me");
request.Q = "is:sent after:2021/3/28 before:2021/3/31";
var result = request.Execute();
Tip Q works just like the search function in the Gmail web application so if you can get that to return what you want just add it to Q
I have the following query to retrieve a User using the GraphServiceClient with .net core 2.
user = await _graphClient.Users[principalName].Request()
.Expand("Extensions")
.GetAsync();
When I run this I get the following error
Microsoft.Graph.ServiceException: Code: generalException Message:
Unexpected exception returned from the service.
This only happens when I have added a OpenTypeExtension to the user using the following code!
extension = new OpenTypeExtension
{
ExtensionName = AzureADExtensions.UserConstants.ExtensionName,
AdditionalData = new Dictionary<string, object>
{
{"OtherEmail", externalUser.Email},
{"OtherRole" , externalUser.Roles.FirstOrDefault()}
}
};
await _graphClient.Users[user.Id].Extensions.Request()
.AddAsync(extension);
I am starting to get really getting fed up with Azure AD now.
I can't seem to add any meta data to my users. Only doing this because otherEmails does not work with the GraphServiceClient and trying to use any other sensible fields gives me this error:
Tenant does not have a SPO license when updating user
Any help would be appreciated.
So for anyone else that come across this, it seems to be an a bug in the service client.
The code below fails
user = await _graphClient
.Users[userId]
.Request()
.Expand("Extensions")
.GetAsync();
But this code works, just by requesting the extensions in a separate call!
user = await _graphClient
.Users[userId]
.Request()
.GetAsync();
var extensions = await _graphClient.Users[user.Id].Extensions.Request().GetAsync();
user.Extensions = extensions;
Hope that save's someone the time I lost on this!
I raise a subscription request as below and the response is:
Subscription validation request failed. Must respond with 200 OK to this request.
Ho do I do I send this response please in UWP?
var result = await request.AddAsync(
new Subscription
{
ChangeType = "created,updated",
NotificationUrl = "https://webhook.azurewebsites.net/notificationClient",
Resource = "/me/mailfolders('inbox')/messages",
ExpirationDateTime = DateTimeOffset.Now.AddMinutes(20),
ClientState = Guid.NewGuid().ToString()
}
);
Ok, I think i need the webhook and the notificationClient, how/ where do i get these values?
Graph webhooks are not possible on UWP, at this time streaming notifications should be used from the outlook 365 api.