2

I've been working with this for hours, I have .png files inside Dropbox and I need to get a raw link to those files so I can use it within my code. How can I retrieve in C# a shared link that I have in Dropbox? I need to retrieve that link so I can use it in my view to assign it to a src parameter for an image. Inside my controller I'm using the CreateSharedLinkWithSettingsArg method which creates shared links for files, but how can I get those links after? This is my code:

List<Dropbox.Api.Files.Metadata> list = new List<Dropbox.Api.Files.Metadata>();
   foreach (var item in list)
            {
                var path = new Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg(item.PathDisplay);
}

I've used something like this and works good but this code throws an exception when the shared link already exists because the following code tries to create a shared link always, I've shared all the files and I just need to get the shared link within my code, how can I do this?:

var sharing = await client.Sharing.CreateSharedLinkWithSettingsAsync(path);
var src = sharing.Url;

I'm using Dropbox.Api v 2.0 and C#

7
  • Trying to understand what you want to achieve. Are you trying to figure out how to check if an item already exists before creating it?
    – Vinod
    Commented Dec 7, 2016 at 16:41
  • @Vinod I've created already shared links for all items, I just need to get those links now in C#, I think that Dropbox.Api should have a method for it, I'm reading about GetSharedLinkFileAsync now
    – AlexGH
    Commented Dec 7, 2016 at 16:43
  • Are you using some sort of a wrapper library (nuget package) to access DropBox api?
    – Vinod
    Commented Dec 7, 2016 at 16:46
  • @Vinod yes, I've installed Dropbox.Api
    – AlexGH
    Commented Dec 7, 2016 at 16:48
  • Looks like you may have to use ListSharedLinksAsync, Please look at its usage here dropbox.github.io/dropbox-sdk-dotnet/html/… and dropbox.github.io/dropbox-sdk-dotnet/html/…. GetSharedLinkFileAsync appears to be deprecated.
    – Vinod
    Commented Dec 7, 2016 at 17:13

1 Answer 1

0

The C# code for getting an existing link is pretty scarce. Even the links mentioned in the comments on this question are dead now. Here is what I came up with. It returns the first shared link if there is one. If there isn't it returns "NA".

 public static async Task<string> GetSharedLink(string folder, string fileName)
{
    string sharedLink = "NA";

    DropboxStuff stuff = new DropboxStuff();

    // Depending on your .Net version
    ServicePointManager.SecurityProtocol =
        SecurityProtocolType.Tls12 |
        SecurityProtocolType.Tls11 |
        SecurityProtocolType.Tls;

    using (var dbx = new Dropbox.Api.DropboxClient(stuff.RefreshToken, stuff.AppKey, stuff.AppSecret))
    {
        try
        {                    
            var link = await dbx.Sharing.ListSharedLinksAsync(folder + "/" + fileName);

            // Get the first shared link, if any.
            sharedLink = link.Links[0].Url;

            // Alternatively, get all links and add them to a List<string>
            //foreach (var item in link.Links.Where(i => i.IsFile))
            // {
            //    // linkList.Add(item.Url);
            // }
            // You will need to return a Task<List<string>> instead of a string
            // (or whatever object type you need to return).

        }
        catch (Exception ex)
        {
            // Log error
        }
    }

    return sharedLink;
}

The DropboxStuff is just a class I made to hold the Dropbox credentials. This has worked out fairly well for me. If you have multiple apps, I suggest passing the object into GetSharedLink. This will make it easier to use the same code with different apps. If you just have one Dropbox app, then the above code will work fine.

Also, the files in question only have one link. If there are multiple links, you can uncomment the code to loop through SharedLinkMetadata and handle according to your needs.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.