question

PabloGlomby-5415 avatar image
1 Vote"
PabloGlomby-5415 asked saldana-msft edited

Sharepoint Client API: Get file name from sharing link

Hi!
I have a CSOM program (desktop app). I can use Graph or REST API if needed.
I have a sharing link like:
https://MYTENANT-my.sharepoint.com/:w:/p/pablo/EWkIoUESiylKrOEglXilrkoBf1eat8RNe35sA0JYvaEzjg

I need to get the file name from this link (or at least the extension).

Using CSOM what I did was:
Microsoft.SharePoint.Client.File file = context.Web.GetFileByGuestUrl(szSharingLinkToGetFileNameFrom);
context.Load(file);
context.ExecuteQuery();
Console.WriteLine("File: " + file.Name);


This works just fine if the user that calls this API is the same user that is logged in.

But suppose my use is MARY and Mary has this sharing link belonging to Pablo.
The only way I could make it work is to use this OneDrive connection string when connecting to OneDrive:
https://MYTENANT-my.sharepoint.com/personal/pablo_MYTENANT_com

But remember that if I am Mary, I don't really know the connection string of other user... parsing the link is not a choice since it smells problematic if the UPN changes...

I did not yet test if the sharing link belongs to another tenant (e.g. and that sharing link is Anyone Can View).

Thanks

sharepoint-devoffice-sharepoint-server-developmentmicrosoft-graph-sites-lists
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Being SharePoint Dev issue (using CSOM and not Graph), i just added the right tags/team to assist further!!

0 Votes 0 ·

Hi @PabloGlomby-5415,

Is there anything update on your issue? If my answer helps you, please click "Accept Answer" and upvote it.

0 Votes 0 ·
MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered PabloGlomby-5415 commented

Hi @PabloGlomby-5415,

You could use Graph api to get file name from sharing link. You could refer to this article: https://docs.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=csharp#code-try-2

First you need to ecode the sharing URL:

 string sharingUrl = "https://onedrive.live.com/redir?resid=1231244193912!12&authKey=1201919!12921!1";
 string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
 string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/','_').Replace('+','-');

With the encodedurl, you could use the endpoint to get the shared file:

 GET /shares/{shareIdOrEncodedSharingUrl}

Below is my test:

49925-image.png



If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


image.png (48.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

unfortunately it does not work if the originator of the sharing link is not the same user that is doing the request.
Here if I am logged as "Pablo" and I am using OneDrive for BI and if I share a file using the "Anyone Can View" permission, then I run that query in Graph Explorer using Pablo, I well get the request... but if I am logged as "Peter" I cannot get the file name (I get a 401 error).

0 Votes 0 ·

Hi @PabloGlomby-5415,

I could reproduce this on my end. This could be that Microsoft Graph doesn't think the user "Peter" has access to the file through the sharing link.

You first need to logon as "Peter" and access this file in the browser through the sharing link to let Graph knows you have access to the file. Then you run the query in Graph Explorer as peter, it would work.

0 Votes 0 ·

Thanks but in the real life this is not a choice because this logic will be used in an end-user product and if John receives a link from Peter, John can only login as John.

0 Votes 0 ·
MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered SvenGloeckner commented

Hi @PabloGlomby-5415,

You may misunderstand me. I understand that Peter can only login as Peter. But peter needs to requested the file in the browser firstly.

Consider this scenario: "Pablo" shared a file use "Anyone with the link can view". Peter received the link from Pablo. Peter would get 401 unauthorized error when run graph api to get the file name. This is normal, because graph doesn't know peter has access to this file. Peter needs to request the file in the browser first, thus graph know he has access to the file. After peter requested the file (with the sharing link) in the browser, he could run graph api query successfully.


As a workaround, you could also make a request to the file in c# with the below code. Thus the user could run the query successfully.


 String name = "user1@teanant.onmicrosoft.com";
 String password = "password";
   
 SecureString securePassword = new SecureString();
 foreach (char c in password.ToCharArray())
 {
     securePassword.AppendChar(c);
 }
 String sharingLink = "https://MYTENANT-my.sharepoint.com/:w:/p/pablo/EWkIoUESiylKrOEglXilrkoBf1eat8RNe35sA0JYvaEzjg";

 var credentials = new SharePointOnlineCredentials(name, securePassword);
 var authCookie = credentials.GetAuthenticationCookie(new Uri(sharingLink));
 var cookieContainer = new CookieContainer();
 cookieContainer.SetCookies(new Uri(sharingLink), authCookie);
 var request = (HttpWebRequest)WebRequest.Create(sharingLink);
 request.CookieContainer = cookieContainer;

 var result = (HttpWebResponse)request.GetResponse();





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I came across this thread while looking for the same solution.
Is there really no way to get this working through standard SharePoint Online C# SDK?

0 Votes 0 ·