How can I use the Get-PnPFile or Get-PnPListItems commands to get data from a URL with the structure /_layouts/15/Doc.aspx?sourcedoc=?

Jairo Javier Baleta Cali 216 Reputation points
2024-09-25T14:13:00.5733333+00:00

Good morning.

I hope you are.

I am trying to get information from files that are in SharePoint document libraries. But many of the URLs I have have the following structure:

https://<tenant>.sharepoint.com/:w:/r/sites/<site url>/_layouts/15/Doc.aspx?sourcedoc=xxxxx-xxxx-xxxx-xxxxx&file=xxxxxxxxxxxxx.docx&action=default&mobileredirect=true

I have not been able to use the sourcedoc or file parameter with the Get-PnPFile or Get-PnPListItems commands.

Is there any way I can get information with SharePoint Online PnP commands from a file that has a URL with this structure?

I hope you can help me.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,791 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,572 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,016 Reputation points Microsoft Vendor
    2024-09-26T01:19:04.19+00:00

    Hi @Jairo Javier Baleta Cali

    Could try to use GetFileByUrl to resolve the issue. Please refer to following script

    $credential = Get-Credential
    Connect-PnPOnline –Url "https://contoso.sharepoint.com" –Credentials $credential
    $ctx = Get-PnPContext  
    $web = Get-PnPWeb 
    Write-Host "Web: " $web.Url
    $item = $web.GetFileByUrl("https://<tenant>.sharepoint.com/:w:/r/sites/<site url>......")
    $ctx.Load($item)
    $ctx.ExecuteQuery()
    Write-Host "Item:" $item.Name
    $ctx = $null
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


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.