SharePoint Online: PowerShell to Get List by URL

Requirement: PowerShell to Get List by URL in SharePoint Online

SharePoint Online: PowerShell to Get List by URL
You can get a list or library from its URL in SharePoint Online using below PowerShell script.
#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"

#Function to Get List from URL
Function Get-ListByUrl([Microsoft.SharePoint.Client.ClientContext]$Ctx, $ListRelativeUrl)
{
    #Get the Web from Context
    $Web = $Ctx.Web
    #Get the Root Folder of the List from Relative URL
    $ListFolder = $web.GetFolderByServerRelativeUrl($ListRelativeUrl)
    $Ctx.Load($ListFolder.Properties)
    $Ctx.ExecuteQuery()
    
    #Get List Name from Root Folder's Properties
    $ListId = [System.guid]::New($ListFolder.Properties["vti_listname"].ToString())
    $List = $Web.Lists.GetById($ListId)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Return $List
}
     
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListRelativeURL = "/sites/marketing/Project Documents"
 
#Setup Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#call the function to get the List from URL
$List = Get-ListByUrl -Ctx $Ctx -ListRelativeUrl $ListRelativeURL
Write-host $List.title
SharePoint Online: PowerShell to Get List by URL SharePoint Online: PowerShell to Get List by URL Reviewed by Salaudeen Rajack on August 03, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.