Sharepoint Client API: Get file name from sharing link

Pablo Glomby 186 Reputation points
2020-12-18T17:12:41.457+00:00

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

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,580 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.
3,136 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,626 questions
{count} vote

2 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,046 Reputation points
    2020-12-21T08:04:20.34+00:00

    Hi @Pablo Glomby ,

    You could use Graph api to get file name from sharing link. You could refer to this article: https://learn.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.

    1 person found this answer helpful.

  2. MichaelHan-MSFT 18,046 Reputation points
    2020-12-23T07:14:12.433+00:00

    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 = "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();  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.