3

Someone has shared a Box.com folder with me using the link. I need to be able to use the C# SDK or REST API to download the documents from their folder.

I have tried all 3 authentication types and have attempted to access with both the C# SDK and REST API.

//SDK attempt
var findFolder = await client.SharedItemsManager.SharedItemsAsync("https://<userWhoSharedWithMe>.box.com/s/<folderHash>");  // notFound
var folder = await client.FoldersManager.GetInformationAsync(findFolder.Id); 
var items = folder.ItemCollection;

//API Attempt
var client = new HttpClient
{
    BaseAddress = new Uri("https://api.box.com")
};
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<bearerToken>");

var response = await client.GetAsync("2.0/folders/<folderId>/items");
var content = await response.Content.ReadAsStringAsync();

Is there any way to programmatically download documents from a box folder that was shared with me via link?

-- Edited 06/04/2019

The folder owner and I have tried various things and it seems the API still will not allow me to see the content of the shared folder. Is there anything the folder owner needs to do to make it visible?

5
  • This might be helpful - stackoverflow.com/a/11076910/969613 Commented May 30, 2019 at 13:07
  • Thanks, i have tried connecting that way, however it seems the folder is not visible to me over the API. Is there a way the folder owner can make it visible? Commented Jun 4, 2019 at 13:31
  • @Daniel - were you able to find a solution for this? I'm in the same boat! If you were able to figure it out or even come up with an alternate approach, I'd appreciate it if you can share it. Commented Feb 4, 2020 at 16:39
  • @TomVaidyan - Sorry I missed this, unfortunately I was not able to resolved this at that time and had to pivot to a different approach. Commented Jun 17, 2020 at 17:23
  • 1
    @Daniel - no worries! I have updated this question with my own answer, in case you or someone else that stumbles on this post go through the same issues that we went through. Cheers! Commented Jun 18, 2020 at 13:55

1 Answer 1

3

Based on the suggestion that I received from a Box employee, I made the following changes.

First the snippet that didn't work as expected:

// DOES NOT WORK

var reader = new StreamReader("box-config.json");
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);

var sdk = new BoxJWTAuth(config);
var token = sdk.AdminToken();
var session = new OAuthSession(token, "N/A", 3600, "bearer");

boxClient = new BoxClient(config, session, asUser: boxUserId);

Secondly, the modified version that worked, allowing me to see the folder that was shared to me and allowed me to traverse its contents:

// THIS WORKS !!!!!!!!

var reader = new StreamReader("box-config.json");
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);

var sdk = new BoxJWTAuth(config);
var token = sdk.UserToken(boxUserId);
boxClient = sdk.UserClient(token, boxUserId);

And for completeness' sake, here's a snippet of code that will allow you to programmatically access a Box folder and traverse its contents:

//folderId <-- You can find this ID by logging into your box account and navigating to the folder that you're interested in accessing programmatically.

var items = await boxClient.FoldersManager.GetFolderItemsAsync(folderId, limit: 5000, offset: 0, autoPaginate: false,
    sort: "name", direction: BoxSortDirection.DESC);

// How many things are this folder?
Console.WriteLine($"TotalCount: {items.TotalCount}");

// Loop through those items
foreach (var item in items.Entries)
{
    // Get info on each item
    var file = await boxClient.FilesManager.GetInformationAsync(item.Id);

    // Print the filename
    Console.WriteLine($"file: {item.Name}");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.