Question

I currently have a program that will upload selected files to a specific folder on SharePoint and give access to specific users based on their emails using the SharePoint CSOM API, however, I am unable to generate the links for people outside of my domain.

For example: I get

"https://domain.sharepoint.com/:f:/r/sites/TestSite/Shared%20Documents/2019/11157/Test%20Upload?csf=1"

instead of

"https://domain.sharepoint.com/:f:/s/TestSite/EmUDng-KzwxEtHKyJ9Okl0MBlS_z8gxGFl4UUjuGXHmR9w"

I am currently retrieving the first link by using the code from here: https://www.c-sharpcorner.com/article/generating-sharing-links-report-and-removing-sharing-links-using-sharepoint-onli/

I have also tried other suggestions I have found on StackExchange such as Manage links for shared files

All examples I have found like the above all result in the incorrect links.

In case it is not clear currently, I am not after an anonymous link or how to create shared links (as I already have this working), I am after the link that would appear here:

enter image description here

Was it helpful?

Solution

I managed to find the solution after just randomly trying everything. What you can do is as follows:

First, create the sharing link in the way I was previously:

SharingResult result = context.Web.ShareDocument(site,
                       email,
                       ExternalSharingDocumentOption.View,
                       false,
                       "Document Shared as test");

Then with the result variable, grab the invitation link of one of the invited users and then modify it to work for any user (as it turns out, specific links are the same for all users other than a parameter on the end)

string link = result.InvitedUsers.FirstOrDefault().
int index = link.IndexOf("?");
if (index > 0) link = link.Substring(0, index);

This results in the correct link I was looking for. I did speak to some people from Microsoft asking about this, so, thanks Microsoft for telling me this was impossible!

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top