jhoerr / box-csharp-sdk-v2

A C# client for the Box API (v2).
http://developers.box.com/docs/
11 stars 15 forks source link

Folders and Files #38

Closed everettevola closed 11 years ago

everettevola commented 11 years ago

I am having an issue that I cannot explain. Not sure if this is something wrong with my code or in the SDK.

I have a folder on Box, let's call it "ParentFolder". Within "ParentFolder", I have another folder called "Subfolder". Within "Subfolder" is a single file. In code, I am attempting to get a handle on "Subfolder" via the following three lines of code:

BoxManager manager = new BoxManager(request.Token.AccessToken); //get handle on parent folder in which to get subfolder Folder folder = manager.GetFolder(request.ParentFolderId); //get subfolder by name folder = folder.Folders.Single(f => f.Name.Equals(request.Name));

This code does not throw any exceptions, but it appears that the folder object returned from the LINQ style query is not being fully populated when returned. The initial GetFolder method returns my parent folder just fine with full attribution. When I get the subfolder using the LINQ style query, I get the subfolder back, but only the Id property is present, all other folder attributes are null. Here is what the folder object resulting from the LINQ style query looks like from the Immediate Window output:

? folder {type: Folder id: 735293596 created_by: created_at: null modified_by: modified_at: null owned_by: name: mapping description: size: 0} base {BoxApi.V2.Model.File}: {type: Folder id: 735293596 created_by: created_at: null modified_by: modified_at: null owned_by: name: mapping description: size: 0} Files: 'folder.Files' threw an exception of type 'System.NullReferenceException' Folders: 'folder.Folders' threw an exception of type 'System.NullReferenceException' FolderUploadEmail: null ItemCollection: null SyncState: Unknown

You will notice that the folder object says there are no files in the folder. I have verified that the folder Id is correct and when I go to Box.com, there is in fact one file in the folder.

Can you help me understand what is up here?

everettevola commented 11 years ago

It appears that the Folders collection on the Folder object is only populating the Id and Name attributes, leaving all others null. I am not sure if this is designed behavior or not, but seems like a defect.

My temporary workaround is to have the following line of code after the LINQy result, which does the trick. Full attribution.

folder = manager.GetFolder(folder.Id);

Here is the entire method where this is being used:

    protected override Response HandleRequest(GetFolderByNameRequest request)
    {
        BoxManager manager = new BoxManager(request.Token.AccessToken);

        //get handle on parent folder in which to get subfolder
        Folder folder = manager.GetFolder(request.ParentFolderId);
        //get subfolder by name
        folder = folder.Folders.Single(f => f.Name.Equals(request.Name));
        //workaround to get full attribution on folder object
        folder = manager.GetFolder(folder.Id);

        return new FolderResponse(folder);
    }
jhoerr commented 11 years ago

Hi Everett,

This behavior is by design, but it can be a little confusing.

(This behavior is per the API. For further reading, this is documented more thoroughly in the Various Issues with GetFolder and GetItems ticket.)

You can peer at most one level deep as a result of a GetFolder or GetItems call. Which is to say that you can't get information a about a folder, its items, and those items' items in a single call.

Does that make sense?

(Edited to add the 'name' to the list of fields returned for the 'mini item'. Removed an example because it was rendered unnecessary.)

everettevola commented 11 years ago

Ok, I get it. But I think you explained it to me backwards. The behavior I am seeing with GetFolder is that all attributes are returned if I leave the optional fields paramater null. If I use a LINQy query, a 'mini item' is returned with Id, Type and Name populated. This explanation is fine with me and follows your basic usage code posted on the github page where you describe getting a 'mini' file and then another line of code to get the full file object.

This is a bit confusing, but having the fields option should make it easier. Thanks again for getting back so quickly!

Edit: The folder object is fully attributed, the ItemCollection.Entries of folder is the 'mini' version. This is the part that was confusing me, thus causing me to have two code lines to get a fully attributed subfolder. It is all good now!

jhoerr commented 11 years ago

I think you explained it to me backwards.

It wouldn't be the first time. :)

The behavior I am seeing with GetFolder is that all attributes are returned if I leave the optional fields paramater null

All attributes of the requested folder, and mini items for each of subfiles and subfolders, yeah?

It occurs to me that you have a couple other options:

Your posted workaround looks fine to me, btw. It's pretty much what you have to do to walk a file tree.

everettevola commented 11 years ago

Thanks, I was updating my comment above when you posted. I am clear now. Thanks again for the help!

jhoerr commented 11 years ago

My pleasure! I'll close this up, but please feel free to let me know if you have any other questions about this.