SharePoint Online: PowerShell to a Get List

Requirement: Get a list in SharePoint Online using PowerShell

PowerShell to Get a SharePoint Online List
Let's get a list in SharePoint Online and count items in list using 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"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName="Project Tasks"
  
#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
    #get list sharepoint online powershell
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of List Items:" $List.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Similarly, you can get all lists from a SharePoint Online site as:
#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"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
  
#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
    #Get All Lists from the Web
    $Lists =$Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Iterate through each list
    ForEach($List in $Lists)
    {
        Write-host $List.Title
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Get a List in SharePoint Online
#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Documents"

#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#powershell get sharepoint online list
$List = Get-PnPList -Identity $ListName

Write-host $List.ItemCount
To get all lists using PnP PowerShell, use:
#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"

#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#Get all sharepoint online lists
$Lists = Get-PnPList

#Get the Title of All Lists
$Lists | Select Title
SharePoint Online: PowerShell to a Get List SharePoint Online: PowerShell to a Get List Reviewed by Salaudeen Rajack on August 11, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.