Thursday, September 19, 2019

How to get List Items from online SharePoint using PnP PowerShell


How to get List Items from online SharePoint using PnP PowerShell
SharePoint Patterns and Practices (PnP) contains a library of PowerShell commands (PnP PowerShell) that allows you to perform complex provisioning and artifact management actions towards SharePoint.
PnP PowerShell


Prerequisite
Please install the below setup files if you are working in Windows 7 the other OS details refer the above PnP link.
1)     SharePoint Online Management Shell

After Installed the “SharePoint Online Management Shell”, you can try the “Connect-PnPOnline” command.

Please install the PnP PowerShell If you get the below error because the PnP module is not available.

 
Try to run this command for SharePoint Online
“Install-Module SharePointPnPPowerShellOnline”

 
If you get the above error, please install the “Windows Management Framework”

2)     Windows Management Framework

After installed the “Windows Management Framework” , again you can run the below command.
“Install-Module SharePointPnPPowerShellOnline”

Sample Code to Get list Items
The below code is applicable to SharePoint Server 2013, SharePoint Server 2016, SharePoint Server 2019, SharePoint Online.
$siteurl="Site URL"

#Connect the SharePoint Site Connect-PnPOnline

Connect-PnPOnline -Url $siteurl

#get List Items using Get-PnPListItem

$listItems= (Get-PnPListItem -List AngularVendor -Fields "Title") 


foreach($listItem in $listItems){ 

   Write-Host "Title" : $listItem["Title"] 
   Write-Host "---------------------------" 
}  

Sample Code to create a new List view
#Config Variables
$SiteURL = ""
$ListName= "TestList"
$ViewName= "TestView"
$ViewFields = @("ID","Title")
$Query = "<Where><Eq><FieldRef Name = 'ID' /><Value Type = 'Number'>5</Value></Eq></Where>"

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL

    #Create New View Add-PnPView
    Add-PnPView -List $ListName -Title $ViewName -Fields $ViewFields -Query $Query -ErrorAction Stop
    Write-host "View '$ViewName' Created Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}


No comments:

Post a Comment

How To Upload Custom List Template And Create List Instance From Custom List Template Using PowerShell In SharePoint 2013

In this article, let us discuss about how to upload a custom list template and creating a list based on that custom template in SharePoint...