Search code examples
microsoft-graph-api

Searching for SharePoint site causes error when looking for _ using the Microsoft Graph API


I want to search for a Sharepoint Site with the string "My_Site" in it - with an underscore. This fails with an error that says that the underscore character is not valid.

My code looks like this:

var sites = await graph.Sites.GetAsync((config) =>
{
    config.QueryParameters.Select = new string[] { "name", "webUrl", "id" };
    config.QueryParameters.Search = "My_Site";
});

If I remove the underscore - the error disappears.

How can I add an underscore to a Microsoft Graph search?


Solution

  • You need to use double quotes for the search query parameter:

    var sites = await graph.Sites.GetAsync((config) =>
    {
        config.QueryParameters.Select = new string[] { "name", "webUrl", "id" };
        config.QueryParameters.Search = "\"My_Site\"";
    });