SharePoint Online: PowerShell to Get List URL
Requirement: PowerShell to get SharePoint Online List URL.
SharePoint Online PowerShell to Get List URL
There may be times when you need to get the URL of a SharePoint Online list or document library. This blog post will show you how to use PowerShell to get the URL of a list in SharePoint Online.
Here is how to get a 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://Crescent.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 -Interactive
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
}
Thank you for this script. If there is a space in the list name, the URL gets split. Is there a way to handle that?
Encode the URL! E.g. [URI]::EscapeUriString($ListURL)