简体   繁体   中英

How to retrieve SharePoint Share Links using CSOM

I am currently developing a program that will upload selected files to a specific folder (and create the folders automatically if necessary) on SharePoint and give access to specific users based on their emails using SharePoint CSOM, however I am unable to retrieve the links for people outside of my domain.

For example, with my current code, I am retrieving links like the following:

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

However, the link I am trying to retrieve is like the following:

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

I can retrieve the link above if I go into SharePoint and manually retrieve it (the program makes the links correctly), but that defeats the purpose of my program. The program is supposed to automate the upload and link retrieval of files from within my company's domain.

I do not want the first link because only people who are within my domain can access the file/folder with it. The 2nd link allows for anyone who has been given access (via an email address) to access the file/folder.

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: https://sharepoint.stackexchange.com/questions/91451/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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM