SharePoint Online: PowerShell to Get List URL

Requirement: PowerShell to Get SharePoint Online List URL

SharePoint Online PowerShell to Get List URL

Here is how to get list URL in SharePoint Online with PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName = "Documents"

#Get Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the Web and List Objects
$Web = $Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()

#List Relative URL
Write-host "List Relative URL:"$List.RootFolder.ServerRelativeUrl

#Get List Full URL
If($Ctx.Web.ServerRelativeUrl -eq "/")
{
    $ListURL = $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
}
Else
{
    $ListURL = $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
}
Write-host "List Full URL:"$ListURL

PnP PowerShell to Get the absolute URL of a List in SharePoint Online

We can obtain the Full URL of a list or library in SharePoint Online using PnP PowerShell, as:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
  
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -UseWebLogin

Try {
    #Get the List
    $List = Get-PnPList $ListName -Includes ParentWeb -ThrowExceptionIfListNotFound -ErrorAction Stop
    
    #Get the absolute URL of the List
    If($List.ParentWeb.ServerRelativeUrl -eq "/")
    {
        $ListURL = [String]::Concat($List.ParentWeb.Url,$List.DefaultViewUrl)
    }
    Else
    {
        $ListURL = [String]::Concat( $List.ParentWeb.Url,$List.DefaultViewUrl.Replace($List.ParentWeb.ServerRelativeUrl,[string]::Empty))
    }
    
    Write-host $ListURL
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply