cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

Share a folder with team members using dropbox api v2.0

Share a folder with team members using dropbox api v2.0

itsrainman
Explorer | Level 3

Hello,

 

I am trying to create an app that will run on a internal remote server. The app 'listens' for XML files being generated by our BMS system and then processes these as required.

 

The next add on for the app, it is already running is to make use of the XML files above to create folders, share the folder with team members and then add in files for the team members to modify.

 

I use Visual Basic and have figuered out to create a folder which then appears in windows explorer. I have put a small delay after the folder creation so that enough time is given for the folder to sync back from dropbox.

 

I next want to share it with one of our team members which i assume i will do via thier email address.

 

So my code so far is,

 

 

Public Shared Sub Main(ByVal args As String())
        Dim GetDetails = Threading.Tasks.Task.Run(CType(AddressOf ProcessingForm.GetID, Func(Of Threading.Tasks.Task)))
        GetDetails.Wait()
End Sub

Private Shared Async Function GetID() As Task(Of Threading.Tasks.Task)
        Using dbx = New DropboxClient("My Token")
            Dim strFolder As String = "/TempFolder"
            Dim full = Await dbx.Users.GetCurrentAccountAsync()
            Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email)
            If Directory.Exists("C:\Users\Somesubfolder\" & strFolder) Then
                ' This path is a directory.

            Else
                Await dbx.Files.CreateFolderAsync(strFolder, False)
                Thread.Sleep(1000)
            End If

            Await ListRootFolder(dbx)
        End Using
    End Function

    Private Shared Async Function ListRootFolder(ByVal dbx As DropboxClient) As Threading.Tasks.Task
        Dim list = Await dbx.Files.ListFolderAsync(String.Empty)

        For Each item In list.Entries.Where(Function(i) i.IsFolder)
            Console.WriteLine("D  {0}/", item.Name)
        Next

        For Each item In list.Entries.Where(Function(i) i.IsFile)
            Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name)
        Next
    End Function

    Public Async Sub CreateANewFolder(ByVal token As String, ByVal path As String)
        Using dbx = New DropboxClient(token)
            Dim created = Await dbx.Files.CreateFolderAsync(path)
        End Using
    End Sub

I run 'Main' which checks I am a user, creates the folder if it does not exisit and then lists all the folders within my dropbox account.

 

Could someone point me in the direction of how to invite team members to this folder so they can edit the files it contains.

 

A code example would be very useful.

 

Many thanks.

 

 

 

10 Replies 10

Greg-DB
Dropbox Staff

To share a folder using the .NET SDK, you should use the ShareFolderAsync method. Then, you can use the AddFolderMemberAsync method to invite users to it, e.g., by specifying their email in a MemberSelector.Email in an AddMember.

 

I'm afraid I don't have sample code for this, but you would use the same dbx client, and access these methods in dbx.Sharing. The documentation linked above covers the expected parameters/types.

itsrainman
Explorer | Level 3

Many thanks for the response. I have looked and coded these but without the actual code that fits within the functions or an example thereof I'm at a loss. I think I understand the function characteristics but not how to call them.

 

So If we used the example, I would pass the function the folder detail, the members to share with etc. But how this is a processed is what I'm at a loss of.

 

Thanks

 

Greg-DB
Dropbox Staff

Calling these functions would work the same way as the ones you already have, e.g., calling ShareFolderAsync would look like:

 

Await dbx.Sharing.ShareFolderAsync(path)

Please give that a try. If you run in to any issues writing it, post what you have and the details of what you're stuck on (e.g., the error message, etc.)

itsrainman
Explorer | Level 3

Hello Greg,

Many thanks for taking the time to respond. I've been on other stuff and have only managed to get back to this today.

 

So i used your suggestion.

Await dbx.Sharing.ShareFolderAsync(path)

Replacing path with my folder strFolder (/TempFolder)

Await dbx.Sharing.ShareFolderAsync(strFolder)

As soon as I tested the code it appered in my dropbox information bubble on the taskbar - great.

 

Now how do i send other users a link to enable them to share this folder. I assume it something along the lines of:

 

 

Await dbx.Sharing.AddFolderMemberAsync(strFolder, "someemail@email.com")

However when i get this there is an error with the message below.

 

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at ....

Inner Exception 1:
InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IEnumerable`1[Dropbox.Api.Sharing.AddMember]'.

 

I think its because its not possible to convert the email address which is a string to an iEnumerable(of AddMember).

 

How do i create a list of email addresses to send the share link to?

 

 

Greg-DB
Dropbox Staff
I just dug up this old post where I wrote an example of using MemberSelector and AddMember to specify a member to add via an email address:

https://www.dropboxforum.com/t5/API-Support-Feedback/How-we-share-folder-and-file-via-dropbox-api/m-...

It also shows how to check the shared folder job, since you need to supply the shared folder ID, not path, when adding a member. Hope this helps!

chirstius
Dropbox Staff

Hello @itsrainman,

 

You might want to take a look at the docs for the AddFolderMemberAsync() method. It takes an AddFolderMemberArg as it's parameter.

I'd try constructing a new AddFolderMemberArg, passing that in, and seeing if things work a little better.

 

Hope that helps,

 

-Chuck

itsrainman
Explorer | Level 3

Many thanks.

 

Took a look and changed the code as required.

            Dim shareFolderLaunch = Await dbx.Sharing.ShareFolderAsync(strFolder)
            Dim sharedFolderId As String = ""
            If shareFolderLaunch.IsAsyncJobId Then

                While True
                    Dim shareFolderJobStatus = dbx.Sharing.CheckShareJobStatusAsync(shareFolderLaunch.AsAsyncJobId.Value)
                    If shareFolderJobStatus.IsFaulted Then
                        Console.WriteLine(("Sharing folder failed: " + shareFolderJobStatus.IsFaulted))
                        'return
                    ElseIf shareFolderJobStatus.status = TaskStatus.Running Then
                        Console.WriteLine("Sharing folder in progress...")
                        ' todo: add some delay
                    ElseIf shareFolderJobStatus.IsCompleted Then
                        Console.WriteLine("Sharing folder complete.")
                        sharedFolderId = shareFolderJobStatus.Id
                        Exit While
                    End If


                End While

            ElseIf shareFolderLaunch.IsComplete Then
                sharedFolderId = shareFolderLaunch.AsComplete.Value.SharedFolderId
            Else
                'Return
            End If

            Console.WriteLine(("Shared folder with ID: " + sharedFolderId))
            Dim members = {New AddMember(New MemberSelector.Email("someemail@email.com"))}
            Await dbx.Sharing.AddFolderMemberAsync(sharedFolderId, members)

All functions and I get an email 🙂

 

Just need to code in some error checks for an already shared folder etc, so getting there.

itsrainman
Explorer | Level 3

Hello again all,

 

I have again had some time to spend on this today and I'm a little confused.

 

I have created a folder, this appears in dropbox and is shared with me.

I next add some files and I can see them if i inspect the folder.

However if I attempt to share the folder with another user via email I get the error "bad_path/already_shared/.."

I am just calling the original code that created the folder and then shared it with the first user - me,. How do i add additional users to the share?

chirstius
Dropbox Staff

Hey @itsrainman,

 

Can you share a code sample of your call and the exact (full output) of the error response?

 

-Chuck

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    chirstius Dropbox Staff
  • User avatar
    itsrainman Explorer | Level 3
What do Dropbox user levels mean?