msgraph-sdk-javascript
msgraph-sdk-javascript copied to clipboard
How is the .search query suppose to work?
According to https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/QueryParameters.md I am supposed to put a string into this function, but what field is this searching on?
For example, what field is is used to match somestring
:
.api('/servicePrincipals')
.search('somestring')
.count(true)
.get();
Perhaps the docs can be updated. AB#8943
@vprasanth Thank you for your question!
-
$search="someValue"
<- This will search some default search properties.
The following example returns all messages in the signed-in user's Inbox that contains "pizza" in any of the three default search properties: GET https://graph.microsoft.com/v1.0/me/messages?$search="pizza"
Refer - https://docs.microsoft.com/en-us/graph/query-parameters#using-search-on-message-collections
2. $search="propertyName:value"
<- This will be used in case a property has search enabled and is not default.
Example - me/messages?$search="body:excitement"
Note - Not all properties support the search query.
Please let me know if you have any questions.
I don't think this is working as expected, I was trying to search on a supported field however kept getting an error regarding an unknown character, which was saying the colon (:) was unknown. So I couldn't use this method.
eg. .search("displayName:somevalue")
didn't work. I tried wrapping the whole thing in quotes, as suggested in #302 but that didn't work either. I also tried escaping the entire string as a last-ditch effort -- nope.
$search="someValue" <- This will search some default search properties.
This info should probably be added to https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/QueryParameters.md. Typically when using this query we would want to search on a specific field since there are no field-specific query params.
@vprasanth
Can you share the full query that you are trying? Which entity are you trying to search?
I tried the following and this worked for me.
client
.api("/users")
.search("\"displayName:SomeValue\"")
.header("ConsistencyLevel","Eventual")
.get()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
and
client
.api("/users")
.search('"displayName:SomeValue"')
.header("ConsistencyLevel","Eventual")
.get()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
I will update the documentation to point the need for including the quotes in the search query.
Hey, thanks for looking into this. My queries were similar to yours, the only difference was I was hitting /servicePrincipals
. I ended up using a different library, so can't reproduce right now, but can try this weekend and get back to you.
Btw, requiring the string to be wrapped seems redundant, and since the other query methods don't require it also feels unintuitive from an API standpoint. I would expect a declarative API like this to take care of this for me -- but perhaps that's just me.
@vprasanth I agree that the framing of the parameters should be handled in a better manner. Marking this issue as a bug.
closing older issue.