cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We've been busy working on some major updates to the Community, so check out what's changing, what’s staying the same and what you can expect from the Dropbox Community right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Getting Parent Directory of Current Folder

Getting Parent Directory of Current Folder

bmilinski
Helpful | Level 6
Go to solution

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?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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! 

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

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! 

bmilinski
Helpful | Level 6
Go to solution

@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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    bmilinski Helpful | Level 6
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?