Sharepoint Client API: Get file name from sharing link

Pablo Glomby 186 Reputation points

I have a CSOM program (desktop app). I can use Graph or REST API if needed.
I have a sharing link like:

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);
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:

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).


Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,773 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,702 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
{count} vote

2 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points

    Hi @Pablo Glomby ,

    You could use Graph api to get file name from sharing link. You could refer to this article:

    First you need to ecode the sharing URL:

    string sharingUrl = "!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:


    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.

    1 person found this answer helpful.

  2. MichaelHan-MSFT 18,016 Reputation points

    Hi @Pablo Glomby ,

    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 = "";  
    String password = "password";  
    SecureString securePassword = new SecureString();  
    foreach (char c in password.ToCharArray())  
    String sharingLink = "";  
    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();