Skip to content

Commit 6858608

Browse files
committed
Update tests and documentation about path root.
1 parent dd48ce2 commit 6858608

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

dropbox-sdk-dotnet/Dropbox.Api/DropboxClient.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,35 @@ public DropboxClient(string oauth2AccessToken, DropboxClientConfig config)
5555
}
5656

5757
/// <summary>
58-
/// Set the value for Dropbox-Api-Path-Root header.
58+
/// Set the value for Dropbox-Api-Path-Root header. This allows accessing content outside of user's
59+
/// home namespace. Below is sample code of accessing content inside team space. See
60+
/// <a href="https://www.dropbox.com/developers/reference/namespace-guide">Namespace Guide</a> for details
61+
/// about user space vs team space.
62+
/// <code>
63+
/// // Fetch root namespace info from user's account info.
64+
/// var account = await client.Users.GetCurrentAccountAsync();
65+
///
66+
/// if (!account.RootInfo.IsTeam)
67+
/// {
68+
/// Console.WriteLine("This user doesn't belong to a team with shared space.");
69+
/// }
70+
/// else
71+
/// {
72+
/// try
73+
/// {
74+
/// // Point path root to namespace id of team space.
75+
/// client = client.WithPathRoot(new PathRoot.Root(account.RootInfo.RootNamespaceId));
76+
/// await client.Files.ListFolderAsync(path);
77+
/// }
78+
/// catch (PathRootException ex)
79+
/// {
80+
/// // Handle race condition when user switched team.
81+
/// Console.WriteLine(
82+
/// "The user's root namespace ID has changed to {0}",
83+
/// ex.ErrorResponse.AsInvalidRoot.Value);
84+
/// }
85+
/// }
86+
/// </code>
5987
/// </summary>
6088
/// <param name="pathRoot">The path root object.</param>
6189
/// <returns>A <see cref="DropboxClient"/> instance with Dropbox-Api-Path-Root header set.</returns>

dropbox-sdk-dotnet/Examples/SimpleTest/Program.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace SimpleTest
99
using System.Threading.Tasks;
1010

1111
using Dropbox.Api;
12+
using Dropbox.Api.Common;
1213
using Dropbox.Api.Files;
1314
using Dropbox.Api.Team;
1415

@@ -118,6 +119,9 @@ private async Task RunUserTests(DropboxClient client)
118119
await Download(client, path, firstFile.AsFile);
119120
}
120121

122+
var pathInTeamSpace = "/Test";
123+
await ListFolderInTeamSpace(client, pathInTeamSpace);
124+
121125
await Upload(client, path, "Test.txt", "This is a text file");
122126

123127
await ChunkUpload(client, path, "Binary");
@@ -313,11 +317,45 @@ private async Task<FolderMetadata> CreateFolder(DropboxClient client, string pat
313317
{
314318
Console.WriteLine("--- Creating Folder ---");
315319
var folderArg = new CreateFolderArg(path);
316-
var folder = await client.Files.CreateFolderAsync(folderArg);
320+
var folder = await client.Files.CreateFolderV2Async(folderArg);
317321

318322
Console.WriteLine("Folder: " + path + " created!");
319323

320-
return folder;
324+
return folder.Metadata;
325+
}
326+
327+
/// <summary>
328+
/// Lists the items within a folder inside team space. See
329+
/// https://www.dropbox.com/developers/reference/namespace-guide for details about
330+
/// user namespace vs team namespace.
331+
/// </summary>
332+
/// <param name="client">The Dropbox client.</param>
333+
/// <param name="path">The path to list.</param>
334+
/// <returns>The <see cref="Task"/></returns>
335+
private async Task ListFolderInTeamSpace(DropboxClient client, string path)
336+
{
337+
// Fetch root namespace info from user's account info.
338+
var account = await client.Users.GetCurrentAccountAsync();
339+
340+
if (!account.RootInfo.IsTeam)
341+
{
342+
Console.WriteLine("This user doesn't belong to a team with shared space.");
343+
}
344+
else
345+
{
346+
try
347+
{
348+
// Point path root to namespace id of team space.
349+
client = client.WithPathRoot(new PathRoot.Root(account.RootInfo.RootNamespaceId));
350+
await ListFolder(client, path);
351+
}
352+
catch (PathRootException ex)
353+
{
354+
Console.WriteLine(
355+
"The user's root namespace ID has changed to {0}",
356+
ex.ErrorResponse.AsInvalidRoot.Value);
357+
}
358+
}
321359
}
322360

323361
/// <summary>

0 commit comments

Comments
 (0)