endsWith filter not supported? - microsoft-graph-api

following:
https://graph.microsoft.com/v1.0/users?$filter=startsWith(displayName,'P')
works like a charm, whereas
https://graph.microsoft.com/v1.0/users?$filter=endsWith(displayName,'z')
throws a 400 error
Any idea why endsWith is not supported ?

Comment moved to answer:
This is a question that has been asked. Although UserVoice has voted a lot, it still does not support filters such as "endswith". see:here.

Unfortunately this is still not working for displayName. But for mail and userPrincipalName the following works (since recently).
Graph explorer:
https://graph.microsoft.com/v1.0//users?$count=true&$filter=endsWith(userPrincipalName,'#example.com')&$select=id,userPrincipalName
Graph.NET SDK:
var request = graphServiceClient.Users
.Request()
.Header("ConsistencyLevel", "eventual")
.Filter("endswith(userPrincipalName,'#example.com')")
.Select(x => new { x.Id, x.UserPrincipalName })
.OrderBy("userPrincipalName");
request.QueryOptions.Add(new QueryOption("$count", "true"));
var result = await request.GetAsync();
See also example 5 at https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=csharp (at this moment the C# example in the docs is incomplete)

Related

Issue adding comments on Google Doc (Done all the work required in retrieving comments from Google Doc)

Could you please help me with an issue I am having adding comments on Google Doc? I have done all the work required in retrieving comments from Google Drive. This is how I retrieve comments from Google Doc:
String fileId ='1up_-hN_z6Fv4NuPUDZioIzFgk42o4j6n8';
String commentId = "AAAAp8ow";
googleUser = await _googleUserSignIn.signIn();
final authHeaders = await googleUser!.authHeaders;
final client = GoogleHttpClient(authHeaders);
print(' passed 1 ');
final marcoDrive = drive.DriveApi(client);
var result = await marcoDrive.comments.get(fileId, commentId, $fields: 'content');
The screenshot below shows the debugging results:
As you can see on the screenshot above, the results variable contains a comment data fetch from Google Doc. The screenshot below shows Google Doc and the orange circle is the comment:
My issue is creating a comment on Google Doc. I find the Comment request parameter, highlighted below using an orange arrow, problematic as I am struggling to create a comment in Google Doc.
var result2 = await marcoDrive.comments.create(request, fileId, $fields: 'content: data is data');
Could you please help me to understand what the Comment request parameter means and how to use it? I would also appreciate it if you could help me understand how to anchor comments.
The programming language I'm using is Dart. Here is a link that I used to help me understand how to manage Google Drive Doc comments: (https://developers.google.com/drive/api/v3/manage-comments).
You have to supply a Comment resource as your request body, as you can see at the official docs: Comments.create.
Here are the fields corresponding to the Comment resource. The only required field is content.
The JSON representation would be something like:
{
"content": "YOUR COMMENT"
}
In Dart, it would probably be something along the following lines:
Comment request = Comment();
request.content = "YOUR COMMENT";
var result = await marcoDrive.comments.create(request, fileId, $fields);
Library reference:
create method
Comment class
Comment constructor

Microsoft Graph Client SDK -filter groups by name

A rather simple ask but I'm having trouble converting my basic httpClient method of querying the Graph into the SDK method. I was using the following and it works fine:
var filter = "IT";
var response = await httpClient.GetAsync($"{webOptions.GraphApiUrl}/beta/groups?$filter=startswith(displayName, '{filter}')&$select=id,displayName");
...now I'm attempting to filter using the SDK as follows:
var groups = await graphServiceClient.Groups
.Request()
.Filter($"displayName startswith {filter}")
.Select("id, displayName")
.GetAsync();
I've also tried .Filter($"startswith("displayName", {filter})) and other variants.
I'm getting an invalid filter clause error. Any ideas?
Apparently it occurs since the provided filter expression for Filter method is invalid, it could be validated like this:
var message = graphServiceClient.Groups
.Request()
.Filter($"displayName startswith '{filter}'")
.Select("id, displayName").GetHttpRequestMessage();
The generated message.RequestUri will return the following value:
https://graph.microsoft.com/v1.0/groups?$filter=displayName startswith '{filter}'&$select=id, displayName}
A valid filter expression needs to be specified like this:
.Filter($"startswith(displayName, '{filter}')")
In case if you want to switch to beta version for GraphServiceClient class, it could be specified like this:
graphServiceClient.BaseUrl = "https://graph.microsoft.com/beta";

How to search the group by the DisplayName using Microsoft Graph?

According to the document, I can list the Office 365 Groups by using the following Graph API:
GET https://graph.microsoft.com/v1.0/groups
I have a C# Web application, and there is a input for searching by the Group DisplayName. Any idea how to query groups based on the DisplayName?
I have tried the following URL: https://graph.microsoft.com/v1.0/groups?$search="displayName:Test" in the MS Graph Explorer which didn't work.
I get the following error.
{
"error": {
"code": "Request_UnsupportedQuery",
"message": "This query is not supported.",
"innerError": {
"request-id": "35d90412-03f3-44e7-a7a4-d33cee155101",
"date": "2018-10-25T05:32:53"
}
}
Any suggestion is welcomed.
Thanks in advance.
According to your description, I assume you want to search the Group by the DisplayName using the search parameters.
Based on this document, we can currently search only message and person collections. So we couldn't use the search parameter.
We can use the filter query parameter to search the Group by DisplayName. For example, we can search the groups whose displayName is start with 'Test',the request url like this:
https://graph.microsoft.com/v1.0/groups?$filter=startswith(displayName,'Test')
Here is C# code that I wrote to get a group using the DisplayName. This code requires a reference to the OfficeDevPnP.Core.
private static async Task<Group> GetGroupByName(string accessToken, string groupName)
{
var graphClient = GraphUtility.CreateGraphClient(accessToken);
var targetGroupCollection = await graphClient.Groups.Request()
.Filter($"startsWith(displayName,'{groupName}')")
.GetAsync();
var targetGroup = targetGroupCollection.ToList().Where(g => g.DisplayName == groupName).FirstOrDefault();
if (targetGroup != null)
return targetGroup;
return null;
}
UPDATE
I see that the answer has already been accepted, but I came across the same issue and found that this answer is out of date. For the next person, this is the update:
The 'search' functionality does work. Whether it was fixed along the way or always has, I am not sure.
'groups' support search,
both the v1 and beta api support search,
search only works on 'displayName' and 'description' fields,
searching on 'directory objects' require a special header: 'ConsistencyLevel: eventual'
Point number 4 is what tripped me up!
Your request would look like this:
https://graph.microsoft.com/v1.0/groups?$search="displayName:Test"
With the request header:
ConsistencyLevel: eventual
There is another catch: You can only specify the first 21 characters and the search always uses 'startsWith'. You're out of luck if you specify more than that: The search always fails.

Drupal 8 Guzzle Format Query String

Forgive me for my ignorance, this is my first attempt at Drupal 8 and I'm not a good php developer to begin with. But I've been reading and searching for hours. I'm trying to do a post using the new Guzzle that replaces the drupal_http_request(). I've done this using Curl but can't seem to get this going in the right direction here. I'm just not "getting it".
Here is a sample of the array I have that pulls data from a custom form. I also tried this with a custom variable where I built the string.
$fields = array(
"enroll_id" => $plan,
"notice_date" => $date,
"effective_date" => $date,
);
$client = \Drupal::httpClient();
$response = $client->post('myCustomURL', ['query' => $fields]);
$data = $response->getBody()->getContents();
try {
drupal_set_message($data);
} catch (RequestException $e) {
watchdog_exception('MyCustomForm', $e->getMessage());
}
This indeed returns the result of REJECTED from my API in $data below - but it doesn't append the URL to included the query => array. I've tried numerous combinations of this just putting the fully built URL in the post (that works with my API - tested) and I still receive the same result from my API. In the end what I'm trying to accomplish is
https://myCustomURL?enroll_id=value&notice_date=12/12/12&effective_date=12/12/12
Any direction or tips would be much appreciated.
Thanks for the responses guys. I was able to get it to work correctly by changing a few things in my post. First changing client -> post to a request('POST', XXX) and then changing "query" to "form_params" as "body" has been deprecated.
http://docs.guzzlephp.org/en/latest/quickstart.html#query-string-parameters
$client = \Drupal::httpClient();
$response = $client->request('POST','https://myURL.html', ['form_params' => $fields]);
$data = $response->getBody()->getContents();
Using $client->post will send a POST request. By looking at the URL that you tested directly you want a GET request.
Either use $client->get or $client->request with the GET parameter. More information and examples in the Guzzle documentation.

How to Add Tag via Asana API

I am trying to do a simple Salesforce-Asana integration. I have many functions working, but I am having trouble with adding a tag to a workspace. Since I can't find documentation on the addTag method, I'm sort of guessing at what is required.
If I post the following JSON to https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tasks:
{"data":{"name":"MyTagName","notes":"Test Notes"}}
The tag gets created in Asana, but with blank notes and name fields. If I try to get a bit more fancy and post:
{"data":{"name":"MyTagName","notes":"Test Notes","followers":[{"id":"MY_USER_ID"}]}}
I receive:
{"errors":[{"message":"Invalid field: {\"data\":{\"name\":\"MyTagName\",\"notes\":\"Test Notes\",\"followers\":[{\"id\":\"MY_USER_ID\"}]}}"}]}
I'm thinking the backslashes may mean that my request is being modified by the post, though debug output shows a properly formatted json string before the post.
Sample Code:
JSONGenerator jsongen = JSON.createGenerator(false);
jsongen.writeStartObject();
jsongen.writeFieldName('data');
jsongen.writeStartObject();
jsongen.writeStringField('name', 'MyTagName');
jsongen.writeStringField('notes', 'Test Notes');
jsongen.writeFieldName('followers');
jsongen.writeStartArray();
jsongen.writeStartObject();
jsongen.writeStringField('id', 'MY_USER_ID');
jsongen.writeEndObject();
jsongen.writeEndArray();
jsongen.writeEndObject();
jsongen.writeEndObject();
String requestbody = jsongen.getAsString();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tags');
req.setMethod('POST');
//===Auth header created here - working fine===
req.setBody(requestbody);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
Any help appreciated. I am inexperienced using JSON as well as the Asana API.
The problem was that I was posting to the wrong endpoint. Instead of workspaces/workspaceid/tags, I should have been using /tags and including workspaceid in the body of the request.
Aha, so you can add tags and even set followers despite the API not mentioning that you can or claiming that followers are read-only.
So to sum up for anyone else interested: POSTing to the endpoint https://app.asana.com/api/1.0/tags you can create a tag like this:
{ "data" : { "workspace": 1234567, "name" : "newtagname", "followers": [45678, 6789] } }
where 1234567 is your workspace ID and 45678 and 6789 are your new followers.
Since you posted this question, Asana's API and developer has introduced Tags. You documentation lays out the answer to your question pretty clearly:
https://asana.com/developers/api-reference/tags
I'm a bit confused by your question. Your ask "how to add a tag" but the first half of your question talks about adding a task. The problem with what you describe there is that you are trying to set a task's followers but the followers field is currently read-only according to Asana's API documentation. That is why you are getting an error. You can not set followers with the API right now.
The second part of your question - with the sample code - does look like you are trying to add a tag. However, right now the Asana API does not support this (at least according to the API documentation). You can update an existing tag but you can't add one.
So, to sum up: at this time the API does not allow you to add followers to a task or to create new tags.

Resources