0

I have four folders, is it possible to access them and get all the files? enter image description here

I already tried this:

jQuery.ajax({
        url: "https://test.sharepoint.com/sites/test/Negocio/Precario/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/test/Negocio/Precario/Precario%20Documentos/Cap%C3%ADtulo%201%20-%20Pre%C3%A7%C3%A1rio%20CEMG')/Files",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
        },
        success: function (data) {
            console.log(data);
        },

        error: function (error) {
            console.log(error);
        }
    });

But only retrieve the "Secretariado Seccao de Empresa" file.

I could access each folder individually but it's not the idea, will it be possible to access them all at once?

2 Answers 2

1

Requesting the Items collection for the library will bring back all items and folders regardless of what folder they are in. You can use the FileSystemObjectType or ContentType property to determine if the item is or is not a folder. In the example below I'm using the FileSystemObjectType.

function getItemsOrFolders(fileSystemObjectType) {
    var message = jQuery("#message");
    message.text("Working on it...");

    var url = appUrl + "/_api//Web/Lists/getByTitle('FolderTest')/Items?" + 
        "$select=*,FileDirRef"

    var call = jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        var fileOrFolderText = "files";
        if (fileSystemObjectType == SP.FileSystemObjectType.folder) {
            fileOrFolderText = "folders";
        }
        message.text("All " + fileOrFolderText + ":");
        message.append("<br/>");
        jQuery.each(data.d.results, function (index, item) {
            if (item["FileSystemObjectType"] == fileSystemObjectType) {
                message.append(String.format(
                    "Title: {0};  Number: {1};  Id: {2}; Path: {3}",
                    item["Title"], item["Number"], item["Id"], item["FileDirRef"]));
                message.append("<br/>");
            }
        });
    });
    call.fail(failHandler);
}
0

Modify the code as below.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl+"/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/test/Negocio/Precario/Precario%20Documentos/Cap%C3%ADtulo%201%20-%20Pre%C3%A7%C3%A1rio%20CEMG')/Files",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
        },
        success: function (data) {
            $.each(data.d.results,function(i,file){
                console.log(file.Name);
            })
        },

        error: function (error) {
            console.log(error);
        }
    }); 
});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.