Microsoft Graph Get All Users Exception Unsupported Query - c#

I can submit this query to Microsoft Graph Explorer but in C# I get the an error message: "Microsoft.Graph.ServiceException: "Code: Request_UnsupportedQuery
Message: Unsupported Query."
var users = await graphClient.Users
.Request()
.Filter("endswith(mail,'#mydomain.com')")
.OrderBy("userPrincipalName")
.GetAsync();

You should send a header ConsistencyLevel=eventual and also $count query parameter to make it work.
To add $count query parameter you can use queryOptions.
List<QueryOption> queryOptions = new List<QueryOption>
{
new QueryOption("$count", true)
};
var users = await graphClient.Users
.Request(queryOptions)
.Filter("endswith(mail,'#mydomain.com')")
.OrderBy("userPrincipalName")
.GetAsync();
The API call what look something like this
https://graph.microsoft.com/v1.0/users?$count=true&$filter=endswith(mail, '#domain.live')&$orderBy=userPrincipalName
You can always test these calls in Graph Explorer.

Related

Filter on onPremisesExtensionAttributes

Iam trying to make Make a GET request to the /users endpoint, using the filter parameter to specify the onPremisesExtensionAttributes value:
var users = await graphClient.Users
.Request()
.Filter($"onPremisesExtensionAttributes/{extensionAttributeName} eq '{extensionAttributeValue}'")
.GetAsync();
but i got error "Microsoft.Graph.ServiceException: 'Code: Request_UnsupportedQuery
Message: Unsupported or invalid query filter clause specified for property 'extensionAttribute14' of resource 'User'."
i can get the uers onPremisesExtensionAttributes values and
filter works fine with other parameters like department of givenName but only show error with onPremisesExtensionAttributes
I had searched alot abut this problem and advanced querties for azure Ad on "https://learn.microsoft.com/en-us/graph/aad-advanced-queries?tabs=csharp"
To make it work you need to add query parameter $count with value true and the header ConsistencyLevel:eventual
List<Option> requestOptions = new List<Option>();
requestOptions.Add(new QueryOption("$count", "true"));
requestOptions.Add(new HeaderOption("ConsistencyLevel", "eventual"));
var users = await graphClient.Users
.Request(requestOptions)
.Filter($"onPremisesExtensionAttributes/{extensionAttributeName} eq '{extensionAttributeValue}'")
.GetAsync();

How to filter Email Messages by Subject Body & HasAttachments using C# Graph API

var inboxMessages = await graphClient.Me
.MailFolders["Inbox"]
.Messages
.Request()
.Select("sender,subject")
.Top(5)
.GetAsync();
I want to filter the messages by subject and hasAttachment using C#
I am seeing few examples like this ,but how we need implement this in C# in above code?
/v1.0/me/messages?$search="subject:search term"
/v1.0/me/messages?$filter=contains(subject, 'my search term')
Can anyone help me out on this.
You can filter messages with attachments based on the subject that contains specific text like this
var subjectText = "your text";
var inboxMessages = await graphClient.Me
.MailFolders["Inbox"]
.Messages
.Request()
.Select("sender,subject")
.Filter($"hasAttachments eq true and contains(subject,'{subjectText}')")
.Top(5)
.GetAsync();
Documentation:
$filter query operator

Include search in Microsoft graph query C#

How do I run the query
https://graph.microsoft.com/v1.0/users?$count=true&$search="displayName:room"&$filter=endsWith(mail,'xxxx.com')&$select=id,displayName,mail
in C#?
this is what I have now:
return await _graphServiceClient
.Users.Request()
.Header("ConsistencyLevel", "eventual")
. .Filter($"(endsWith(mail, 'xxxx.com'))&$count=true")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
Try this code pls:
var queryOptions = new List<QueryOption>()
{
new QueryOption("$count", "true"),
new QueryOption("$search", "\"displayName:tiny\"")
};
var res = await graphClient
.Users.Request(queryOptions)
.Header("ConsistencyLevel", "eventual")
.Filter("endswith(mail,'contoso.com')")
.OrderBy("userPrincipalName")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
When we follow the official code snippet, we should use .Search() but it will meet exception:
Then let's see github issue here, and we can set search parameter into query option.

QueryOptions ignored when getting calendar events in MSGraph Dot Net

I'm trying to get the user's calendar events for today. So I added some query parameters but they're getting ignored and the graph client returns the user's events as if I didn't supply any parameters (startatetime):
var options = new QueryOption[]
{
new QueryOption("startdatetime", DateTime.UtcNow.ToString("o")),
new QueryOption("enddatetime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.Calendar
.Events
.Request(options)
.GetAsync();
I tested it in the graph explorer and it works fine. But in the sdk, it returns calendar events that started before today.
Your code is the equivalent of calling:
`/events?startdatetime={dateTime}&enddatetime={dateTime}`.
That is a valid endpoint, but you're passing invalid query params. What you're looking for is calendarView:
`/calendarView?startdatetime={dateTime}&enddatetime={dateTime}`
Using the SDK, this would look like this:
var options = new QueryOption[]
{
new QueryOption("startDateTime", DateTime.UtcNow.ToString("o")),
new QueryOption("endDateTime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.CalendarView
.Request(options)
.GetAsync();

Microsoft Graph .Net API: Shared OneDrive folder

I'm getting an invalid request error for the following (Message: One of the provided arguments is not acceptable):
DriveRecipient[] invitees = new DriveRecipient[1];
invitees[0] = new DriveRecipient()
{
Email = "testEmail#testdomain.com"
};
var test = await graphClient
.Me
.Drive
.Root
.ItemWithPath("/TestFolder")
.Invite(invitees, true, sendInvitation : true, message: "Test Message")
.Request()
.PostAsync();
I'm trying to share a folder (root/TestFolder) in OneDrive but am getting an invalid request error. Is it possible to share a folder this way? Or alternatively, how would I just create a shared folder, if this doesn't work?
You need to include the roles you want to apply ("read" and/or "write"):
var invitees = new List<DriveRecipient>();
invitees.Add(new DriveRecipient()
{
Email = "testEmail#testdomain.com"
});
var test = await client
.Me
.Drive
.Root
.ItemWithPath("/TestFolder")
.Invite(recipients: invitees,
requireSignIn: true,
sendInvitation: true,
message: "Test Invite",
roles: new List<string>() { "Read", "Write" })
.Request()
.PostAsync();

Categories