Searching with Microsoft Graph Client Library for .NET

Using the MS Graph SDK for .NET is convenient but I find the documentation and examples pretty lacking. So when wanted to use the Search endpoint it took me quite a while to figure out how to make it work. Here is an example showing how to search for SharePoint list items:

Notable gotchas:

  • You must specify all fields you want to return. For custom fields  you must use the Managed Property names (ending with OWS*) for custom fields. Tip: Use the SharePoint Search Query Tool to see all available fields you can use in a query
  • Search returns SearchHit  objects. You need to cast the SearchHit results to the correct type, in this case ListItems . Note though that the casted ListItem will only contain data in the dynamic AdditionalData/Fields structure, everything else will be empty.
  • PageIterator  does not work with Search, so you need to do pagination manually.
  • You can specify the number of items to return with the Size  property. Default is 25, max is 1000. 200 is “reasonable” according to docs. Docs also says that best practice is to start small and increase subsequent requests.
  • In the response, fields will retain their casing from the request – EXCEPT for the first letter which is always lower case…
This entry was posted in Development, Tips and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *