Jay Dave
Jay Dave

Reputation: 981

Unable to view child elements from OneDrive C# SDK

I am coding against the OneDrive C# SDK and I was shared a folder which contains multiple files. When accessing the shared folder from the onedrive.com, I am able to view the files -- however when trying to check the Item the children count is always at zero. I am assuming this may be some mix up on my end or permissions issue -- but I just wanted to run it past for a sanity check.

Code:

private async Task GetItem(string id = null)
{
    List<string> idsToSearch = new List<string>();
    var expandValue = this.clientType == ClientType.Consumer
            ? "thumbnails,children(expand=thumbnails)"
            : "thumbnails,children";
    try
    {
        Item folder;

        if (id == null)
        {
            folder = await this.oneDriveClient.Drive.Root.Request()
                           .Expand(expandValue).GetAsync(); //root
        }
        else
        {
            folder = await this.oneDriveClient.Drive.Items[id].Request()
                           .Expand(expandValue).GetAsync(); //children of root
        }

        WriteToFile(new List<string>(new[] { @"Folder: " + folder.Name }));

        if (folder.Children.Count == 0)
        {
            WriteToFile(new List<string>(new[] { @"NO Children" }));
        }
        else
        {
            foreach (var child in folder.Children)
            {
                WriteToFile(new List<string>(new[] { 
                @"Children of " + folder.Name + " : " + child.Name }));
            }

            foreach (var item in folder.Children)
            {
                GetItem(item.Id);
                idsToSearch.Add(item.Id);
            }
        }
    }
    catch (Exception exception)
    {
        PresentServiceException(exception);
    }
}

I also included a snapshot of the Item object when it reaches the Shared folder object:

enter image description here

Update

After looking through the folder object some more I found that there is RemoteItem which is returning the correct number of child counts -- however does not have any meta data to fetch the child elements.

enter image description here

Upvotes: 2

Views: 460

Answers (1)

Brad
Brad

Reputation: 4192

From the comments on the question it was determined that this is a RemoteItem scenario. Remote items are different to local items - while there's some local metadata that's useful for rendering, the actual metadata for the item lives in another user's drive. Therefore, when such an item is encountered it may be necessary (e.g. if you need to enumerate children of a remote folder) for a subsequent request needs to be made directly for the item in question (using the driveId from the remoteItem.parentReference and the id from remoteItem.Id).

Have a look at this documentation for some more information.

Upvotes: 1

Related Questions