question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PnPContext.Web does not provide GetFileByUrl

See original GitHub issue

Category

  • Bug

Describe the bug

It seems like the PnPContext.Web instance does not provide the method GetFileByUrl. I need that method because it works flawlessly with different sharepoint links (Share with people who have access link, direct link etc.). I can use this method manually using PnPContext.Site.ExecuteRequestAsync + matching ApiRequest instance like so:

var request = new ApiRequest(ApiRequestType.SPORest, $"_api/web/GetFileByUrl('{my_url}')");
var response = await context.Site.ExecuteRequestAsync(request);

var model = JsonDocument.Parse(response.Response);

Any possibility to parse this model or response at least into a IFile instance? So that I can work normally with the GetContentAsync function?

Expected behavior

GetFileByUrl is available through the Web instance property of the PnPContext

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
LegendaryBcommented, Oct 7, 2021

@jansenbe Seems like I had some error in my previous code. Now it really works. Code which works (but is currently still ugly^^):

var context = await _pnpContextFactory.CreateAsync(new Uri("https://tenant.sharepoint.com"));

string base64Value = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(documentLink.Url.ToString()));
string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');

var request = new ApiRequest(ApiRequestType.Graph, $"shares/{encodedUrl}/driveitem");

var response = await context.Site.ExecuteRequestAsync(request);

var x = JsonDocument.Parse(response.Response);
var y = x.RootElement.GetRawText();

Now I just need to take the @microsoft.graph.downloadUrl value and remove everything after the _layouts to get the site url. Thank you!

1reaction
LegendaryBcommented, Oct 7, 2021

Hey @jansenbe,

thank you for the fast response. Yeah I know that this method exist. But GetFileByUrl can work with share links without problems.

For example this link: https://tenant.sharepoint.com/:b:/r/sites/hr/Shared%20Documents/contoso_report.pptx or https://tenant.sharepoint.com/:p:/g/EVJ5zTXVJ0pGnTUuYQ3mlYIByIJRTNy1Sbtzme-CZ9D7lQ

So the benefit is that I can just use the share link and then download the document.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SPO - Get file by URL (generated by "Get Link")
Recently found the solution by myself (partly using PnP) - function GetFileByUrl does the job. cls $credential = Get-Credential ...
Read more >
pnpcore/docs/using-the-sdk/files-intro.md at dev
The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working...
Read more >
Get-PnPContext | PnP PowerShell
Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. If not provided, the ...
Read more >
Working with webs | PnP Core SDK
The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working...
Read more >
PnP Core .Net Access subfolder inside my Documents ...
Currently implementing Code to Access a specific Folder called "Customer" which is inside my Documents Folder in Sharepoint. Whatever I tried it ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found