Selim Alawwa
Selim Alawwa

Reputation: 762

Can't list folders using SharePoint REST API

Based on Microsoft Sharepoint Documentation Here I am trying to list my Sharepoint application document libraries & folders using the REST API.

I have tried by Java using apache HTTP client and using Postman in both ways failing with status code 400 and error message: Value does not fall within the expected range.

Below is the response error messages:

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>-2147024809, System.ArgumentException</m:code>
    <m:message xml:lang="en-US">Value does not fall within the expected range.</m:message>
</m:error>

Here is a screenshot of the postman:

enter image description here

I am using NTLM Authentication

Here is my java code sample (test case):

@Test
public void testListFolders() {
    // SharePoint site URL and API endpoint
    String siteUrl = "http://sharepoint2013.MY_SITE_NAME.com/sites/home";
    String apiEndpoint = "/_api/web/GetFolderByServerRelativeUrl('/Shared%20Documents')/Folders";

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY),
            new NTCredentials(String.join(":", "MY_USERNAME", "MY_PASSWORD")));

    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setExpectContinueEnabled(false)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
            .setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
            .build();

    // Create HttpClient instance
    CloseableHttpClient httpClient = HttpClients.custom()
                                                .setDefaultCredentialsProvider(credsProvider)
                                                .setDefaultRequestConfig(defaultRequestConfig)
                                                .build();
    // Create HTTP GET request
    HttpGet request = new HttpGet(siteUrl + apiEndpoint);
    request.setHeader(HttpHeaders.ACCEPT, "application/json;odata=verbose");

    try {
        // Execute the request
        HttpResponse response = httpClient.execute(request);
        System.out.println("Status code: " + response.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The status code is always 400 and same error message as form Postman.

If I replace the endpoint for this test case to be the endpoint to list Sharepoint list: /_api/web/Lists/?$Select=Title%2CIsApplicationList%2CContentTypesEnabled%2CHidden%2CBaseType%2CItemCount%2CIsCatalog&$Filter=BaseType%20eq%200%20and%20Hidden%20eq%20false it will work fine and return a valid response and status code 200

Upvotes: 0

Views: 538

Answers (1)

Muhammad Murad Haider
Muhammad Murad Haider

Reputation: 1467

you may need separate API calls for this. you can get lists/document libraries using '_api/web/lists' endpoint. Then for each list you can hit 'list/getbytitle('listname')/items' endpoint to get list's items. Here the catch is, you can use filter parameter to get only the folders i.e. '$filter= 'FileSystemObjectType = 1'. This should return you only the folders in the response.

Upvotes: 0

Related Questions