Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
bmilinski
6 years agoHelpful | Level 6
Getting Parent Directory of Current Folder
I am trying to retrieve the parent folder of the current folder I am in to make it easier to trace permissions amongst folders. I currently am able to list every user's folder, permissions, etc., but is there anyway to use the "item" variable in the code below to directly access its parent folder's name?
using (DropboxTeamClient DBTeamClient = new DropboxTeamClient("MYACCESSKEY"))
{
//get all the dropbox members
var members = await DBTeamClient.Team.MembersListAsync();
//loop through all members ordered by email alphabetical
foreach (var member in members.Members.OrderBy(a => a.Profile.Email))
{
int count = 0;
//get each user
var userClient = DBTeamClient.AsMember(member.Profile.TeamMemberId);
//actions to check
var actionsToCheck = new FolderAction[] { FolderAction.EditContents.Instance, FolderAction.InviteEditor.Instance };
//get each user's file information
var list = await userClient.Sharing.ListFoldersAsync(actions: actionsToCheck); // actions can optionally be supplied to check the permissions the user has for specific actions
//loop through the list of file and show permissions on folders
foreach (var item in list.Entries)
{
int length = item.AccessType.ToString().Length - 32 - 2;
System.Diagnostics.Debug.WriteLine("");
System.Diagnostics.Debug.WriteLine(member.Profile.Name.DisplayName);
System.Diagnostics.Debug.WriteLine(item.Name);
System.Diagnostics.Debug.WriteLine(item.AccessType.ToString().Substring(32, length));
System.Diagnostics.Debug.WriteLine(count + "");
count++;
}
System.Diagnostics.Debug.WriteLine(count + " total folders");
}
}
Thanks in advance!
EDIT: I noticed item contains a "ParentSharedFolderId" member, but how can I use this to find the name of the parent?
Yes, there are a few options here, depending on exactly what you're looking for. I've illustrated and commented on them in code:
// get the name of the parent shared folder, if any // note that the parent shared folder is not necessarily the immediate parent folder of the item if (item.ParentSharedFolderId != null) { Console.WriteLine("item ParentSharedFolderId: " + item.ParentSharedFolderId); var parentSharedFolderMetadata = await userClient.Sharing.GetFolderMetadataAsync(item.ParentSharedFolderId); Console.WriteLine("item ParentSharedFolder Name: " + parentSharedFolderMetadata.Name); } // get the parent folder name by taking the next to last path component if (item.PathLower != null) { var pathComponents = item.PathLower.Split('/'); Console.WriteLine("item parent component: " + pathComponents.ElementAtOrDefault(pathComponents.Count() - 2)); // more ideally, you would pass the path string into some path library to do the parsing for you. } // get the parent folder name from the metadata // this was recently added to the API, but isn't available in the .NET SDK yet. it should be included with the next release of the SDK //if (item.ParentSharedFolderId != null) //{ // Console.WriteLine(item.ParentFolderName); //}Hope this helps!
2 Replies
Replies have been turned off for this discussion
- Greg-DB6 years ago
Dropbox Community Moderator
Yes, there are a few options here, depending on exactly what you're looking for. I've illustrated and commented on them in code:
// get the name of the parent shared folder, if any // note that the parent shared folder is not necessarily the immediate parent folder of the item if (item.ParentSharedFolderId != null) { Console.WriteLine("item ParentSharedFolderId: " + item.ParentSharedFolderId); var parentSharedFolderMetadata = await userClient.Sharing.GetFolderMetadataAsync(item.ParentSharedFolderId); Console.WriteLine("item ParentSharedFolder Name: " + parentSharedFolderMetadata.Name); } // get the parent folder name by taking the next to last path component if (item.PathLower != null) { var pathComponents = item.PathLower.Split('/'); Console.WriteLine("item parent component: " + pathComponents.ElementAtOrDefault(pathComponents.Count() - 2)); // more ideally, you would pass the path string into some path library to do the parsing for you. } // get the parent folder name from the metadata // this was recently added to the API, but isn't available in the .NET SDK yet. it should be included with the next release of the SDK //if (item.ParentSharedFolderId != null) //{ // Console.WriteLine(item.ParentFolderName); //}Hope this helps!
- bmilinski6 years agoHelpful | Level 6
Greg-DB Thank you for the quick reply and useful information. I ran the code, but got the following error:
"Exception User-Unhandled: Dropbox.Api.ApiException`1: 'not_a_member/..'
on this line of your code:
var parentSharedFolderMetadata = await userClient.Sharing.GetFolderMetadataAsync(item.ParentSharedFolderId);
Is there anyway to avoid this exception from occurring? Or does this have to do with how the folders are setup in my company's Dropbox?
EDIT: The error is being caused by some folders who don't have parent directories, so I added a try/catch statement to catch this exception. It seems to catch all other parent folders and works correctly, thanks for the help.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!