I'm using the Microsoft Graph SDK v5.61 in a .NET application to retrieve a specific user's mail list, and I want to order the messages by size in descending order. Here is the code I'm using:
var findItems = graphserviceclient.Users[userID].MailFolders["inbox"].Messages.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Orderby = new string[] { "size" };
}).GetAwaiter().GetResult();
However, this results in the following error:
Microsoft.Graph.Models.ODataErrors.ODataError: 'Could not find a property named 'size' on type 'microsoft.graph.message'.'
It seems like the size property isn't available for sorting on microsoft.graph.message. I've checked the documentation but couldn't find clarity on whether this is supported or an alternative approach to achieve this.
My Question:
Is there a way to sort messages by size using Microsoft Graph SDK? If sorting by size directly is not possible, is there a recommended workaround for this use case? Any help or guidance would be appreciated. Thank you!
To get the message size, you need to expand the PidTagMessageSize extended property. Graph API doesn't expose the size of the message by default. It leads to the issue with ordering messages by size. Extended properties doesn't support ordering. You will have to read all messages and sort them locally.
It should be quite easy:
public struct MessageInfo
{
public string Id { get; set; }
public long Size { get; set; }
}
// get first page
var result = graphserviceclient.Users[userID].MailFolders["inbox"].Messages.GetAsync((rc) =>
{
rc.QueryParameters.Expand = new string[] { "SingleValueExtendedProperties($filter=Id eq 'LONG 0x0E08')" };
rc.QueryParameters.Select = new string[] { "id", "subject" };
rc.QueryParameters.Top = 999;
}).GetAwaiter().GetResult();
var messages = new List<MessageInfo>();
messages.AddRange(result.Value.Select(m => new MessageInfo { Id = m.Id, Size = Convert.ToInt64(m.SingleValueExtendedProperties[0].Value) }));
// iterate through the all pages
var pageIterator = PageIterator<Message, MessageCollectionResponse>.CreatePageIterator(graphserviceclient, result, (message) =>
{
messages.Add(new MessageInfo { Id = message.Id, Size = Convert.ToInt64(message.SingleValueExtendedProperties[0].Value) });
return true;
});
pageIterator.IterateAsync().GetAwaiter().GetResult();
// order messages by size
var orderedMessages = messages.OrderByDescending(m => m.Size);
I've specified $select
to return only id
and subject
to reduce the size of the response returned by the Graph API.