Graph API: How can i search in SharePoint Folders that contain special characters like '{'?

Mira Lorenz 5 Reputation points
2024-07-11T12:21:45.41+00:00

We are using the Graph API to search for files on SharePoint. This works as expected, unless the Folder the search is running in contains special characters like '{ Test'.
However the problem only seems to exists for some characters and not all.

Using \u007B will result in the char being corretly transformed to '{' but still no result is found.

Other ways of displaying the char like %7B or %F0%9F%98%A7 do not work either and also the char is not converted correctly here.

If we run the search on the parent folder of '{ Test', all files are found and listed, also those that are contained in '{ Test'

Is there a different way to ensure that all characters are correctly encoded and get recognized correctly when running a search?

{
    "requests": [
        {
            "entityTypes": [
                "listItem"
            ],
            "query": {
                "querystring": "((parentlink:\"SharepointTest/{ Test\") AND (path:\"https://[SharePointHost].sharepoint.com/sites/TestSite/Freigegebene Dokumente/\"))"
            },
            "sortProperties": [
                {
                    "name": "FileName",
                    "isDescending": false
                }
            ],
            "from": 0,
            "size": 500
        }
    ]
}
			
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,339 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,874 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Maycon Batista 336 Reputation points
    2024-07-11T15:06:56.4966667+00:00

    Hi Mira,

    When using the Graph API to search for files on SharePoint, special characters in folder names can indeed cause issues. Here are some steps and best practices to ensure that all characters are correctly encoded and recognized:

    1. URL Encoding: Ensure that all special characters are properly URL encoded. For example, the character { should be encoded as %7B. However, as you mentioned, this might not always work as expected.

    Double Encoding: Sometimes, double encoding the special characters can help. For example, { can be encoded as %257B (where %25 is the encoded form of %).

    Using Unicode: While using Unicode like \u007B should work, it might not always be recognized correctly by the API. Ensure that the API endpoint and the client both support Unicode.

    Graph API Search Query: When constructing the search query, make sure to encode the entire query string properly. Here is an example of how to encode the search query:

    JavaScript

    function searchFiles(folderName) {
        var encodedFolderName = encodeURIComponent(folderName);
        var searchQuery = "path:" + encodedFolderName + " AND filetype:docx";
        var url = "https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root/search(q='" + encodeURIComponent(searchQuery) + "')";
        $.ajax({
            url: url,
            method: "GET",
            headers: {
                "Authorization": "Bearer " + accessToken,
                "Accept": "application/json"
            },
            success: function(data) {
                console.log("Search results:", data);
            },
            error: function(error) {
                console.log("Error:", error);
            }
        });
    }
    
    

    Testing with Different Characters: Test the search functionality with different special characters to identify which ones cause issues. This can help in creating a more robust encoding strategy.

    API Documentation and Support: Refer to the Graph API documentation and community forums for any updates or known issues related to special character handling.

    If there are any misunderstandings, please let me know

    Best regards,

    Maycon Novaes

    If the Answer is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.