Office 365 SDK for Android; How to list files under a folder?

291 Views Asked by At

Hello StackOverflow pals,

I'm using Office-365-SDK for Android ( https://github.com/OfficeDev/Office-365-SDK-for-Android ), I've been looking on all SDK examples (https://msdn.microsoft.com/en-us/office/office365/howto/starter-projects-and-code-samples) and also I've been looking into SDK source code directly but I'm not able to figure how to list files under a folder; all examples does only file list under root folder.

On Office365 REST API I can see clearly that there's a call for this pourpose ( https://msdn.microsoft.com/office/office365/APi/files-rest-operations#FolderoperationsListfoldercontentsREST ) but on this SDK I didn't found a way to create the same call.

My actual code is the very same than the one on SDK code snippets ( https://github.com/OfficeDev/O365-Android-Snippets/blob/master/app/src/main/java/com/microsoft/office365/snippetapp/Snippets/FileFolderSnippets.java ) on call "getFilesAndFolders". It list properly files and folder under root, but I haven't a way to list files and folder under a concrete folder, so I cannot create a file explorer :(.

Thanks in advance! Regards.

2

There are 2 best solutions below

0
On BEST ANSWER

Proper way to do it:

//retrieve the folder (as item)             
Item item = client.getFiles().getOperations().getByPath("foo/path").get();
//get the folder, and get the children 
client.getFiles().getById(item.getId()).asFolder().getChildren().read();

Marco Torres from Microsoft have answered this question on one ticket that I've opened on SDK github -> https://github.com/OfficeDev/Office-365-SDK-for-Android/issues/88

Hope it helps to someone :)

0
On

I've found a way to achieve this, isn't an elegant one but maybe can help to someone, take a look into this code example:

public ChildsSharePointClient getFileClient(String appendPath) {

    if (mServices == null || mServices.isEmpty()) {
        mCountDownLatch = new CountDownLatch(1);
        discoverServices();
        try {
            mCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    if (mFileServiceInfo == null) {
        mFileServiceInfo = getService(Constants.MYFILES_CAPABILITY);
    }

    String serviceEndpointUri = mFileServiceInfo.getserviceEndpointUri();
    String serviceResourceId = mFileServiceInfo.getserviceResourceId();

    if (!TextUtils.isEmpty(appendPath)) {
        serviceEndpointUri += appendPath;
    }

    AuthenticationManager.getInstance().setResourceId(serviceResourceId);
    DefaultDependencyResolver dependencyResolver = (DefaultDependencyResolver) AuthenticationManager.getInstance()
            .getDependencyResolver();

    return new ChildsSharePointClient(serviceEndpointUri, dependencyResolver);
}

public List<Item> getFilesList(String folderId) {

    List<Item> filesAndFolders = null;
    try {

        if (TextUtils.isEmpty(folderId)) {
            folderId = ROOT_PATH;
        }

        filesAndFolders = getFileClient(FILES_PATH + "/" + folderId)
                .getChilds()
                .read()
                .get();

        LOG.debug("Retrieved {} elements",filesAndFolders.size());

    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    return filesAndFolders;

}

/**
 * Created by mike on 16/06/15.
 */
public class ChildsSharePointClient extends SharePointClient {

    /**
     * Instantiates a new SharePointClient.
     *
     * @param url      the url
     * @param resolver the resolver
     */
    public ChildsSharePointClient(String url, DependencyResolver resolver) {
        super(url, resolver);
    }

    /**
     * Gets Item.
     *
     * @return the Item
     */
    public ODataCollectionFetcher<Item, ItemFetcher,     ItemCollectionOperations> getChilds() {
        return new ODataCollectionFetcher<Item,     ItemFetcher,ItemCollectionOperations>("children", this,     Item.class,ItemCollectionOperations.class);
    }

}

Basically I'm initiating SharePointClient directly with url with id of desired folder and I've added a getChild() action into a class that inherit from MS SharePointClient and request for "children" item.

Hope it helps to someone in the meanwhile we found a more elegant solution.