简体   繁体   中英

400 error when filtering results with Graph API

Given these URLs:

const urls = {
    demos: GRAPH_ENDPOINT + `sites/${ids.siteMain}/lists/${ids.listDemos}/items?expand=fields`,
    demosScheduled: GRAPH_ENDPOINT + `sites/${ids.siteMain}/lists/${ids.listDemos}/items?expand=fields&$filter=fields/isScheduled eq 1`,
}

The first one works fine. It lists all entries in the sharepoint list. The second one, however, returns a 400 (Bad Request) error. In the debug console, it looks like this:

/items?expand=fields&$filter=fields/isScheduled%20eq%20%271%27

I have tried it with and without quotes ( ' ). It works both ways in the Graph Explorer , and only returns the expected results. As a matter of fact, if I copy the full URL from the debug console and paste it in graph explorer, it works even with HTML encoding.

Relevant Code:

function callMSGraph(endpoint, token, callback) {
    const headers = new Headers();
    const bearer = `Bearer ${token}`;

    headers.append("Authorization", bearer);

    const options = {
        method: "GET",
        headers: headers
    };

    fetch(endpoint, options)
        .then(response => response.json())
        .then(response => callback(response, endpoint))
        .catch(error => console.log(error));
}

function getDemos() {
    getTokenPopup(tokenRequest)
        .then(response => {
            //this does not work
            callMSGraph(urls.demosScheduled, response.accessToken, ui.displayDemos);
            //this does work
            callMSGraph(urls.demos, response.accessToken, ui.displayDemos);
        }).catch(error => {
            console.error(error);
        });
}

Could you please try using the JavaScript SDKs , and use the below query to run the same

let items = await client.api('/sites/root/lists/list_id/items?expand=fields&$filter=fields/Title+eq+\'Help+request\'')
    .header('Prefer','HonorNonIndexedQueriesWarningMayFailRandomly')
    .filter('fields/isScheduled eq \1 ')
    .get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM