question

venkateshpadmanabhan-5594 avatar image
0 Votes"
venkateshpadmanabhan-5594 asked WendyLi-MSFT commented

Powershell : Retrieve sharepoint list data

Hi.
I am trying to export the SharePoint list data to CSV by following this link :

https://www.sharepointdiary.com/2013/04/export-sharepoint-list-items-to-csv-using-powershell.html


I am following the last example given. i am trying the code as :

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Variables

$SiteUrl="sharepoint site"
$ListName="list name"
$OutPutFile = "C:\path\ListData.csv"


Delete the Output File if exists

If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }

Get Web and List Objects

$Web = Get-SPWeb $SiteUrl
$List = $Web.Lists[$ListName]
Write-host "Total Number of Items Found:"$List.Itemcount

I am getting error while running in powershell as Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.

How to fix this ?
Thanks

Update 1: I have tried this :
PS C:\WINDOWS\system32> Install-Module -Name Microsoft.Online.SharePoint.PowerShell

PS C:\WINDOWS\system32>
Add-PSSnapin Microsoft.SharePoint.PowerShell
Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.SharePoint.PowerShell' is not installed on this computer.
At line:2 char:1
+ Add-PSSnapin Microsoft.SharePoint.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShell:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand


I do not see a folder named Microsoft.SharePoint.Powershell in the C:\Windows\Microsoft.NET\assembly\GAC_MSIL path.
Can powershell be run from client machine, where sharepoint is not installed ?

windows-server-powershelloffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sadomovalex avatar image
0 Votes"
sadomovalex answered

those example which you provided uses server object model PowerShell cmdlets - they work only with Sharepoint on-prem (not with Sharepoint online) and only when this PowerShell runs on the server which is part of Sharepoint farm.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

EchoDu-MSFT avatar image
0 Votes"
EchoDu-MSFT answered WendyLi-MSFT commented

Hi @venkateshpadmanabhan-5594 ,

The code you provided is for SharePoint on-prem and not for SharePoint Online. It is recommended that you can run the following PowerShell script as an admin:

Export SharePoint Online List Items to CSV using PowerShell

 #Config Parameter
 $SiteURL = "https://domain.sharepoint.com/sites/sitename"
 $ListName = "listname"
 #InternalName of the selected fields
 $SelectedFields = @("ID","Title","Choice1","Person1","Date1") 
 $CSVPath = "C:\Temp\ListData.csv"
 $ListDataCollection= @()
     
 #Connect to PnP Online
 Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
 $Counter = 0
    
 #PageSize:The number of items to retrieve per page request
 $ListItems = Get-PnPListItem -List $ListName -Fields $SelectedFields -PageSize 2000
     
 #Get all items from list
 $ListItems | ForEach-Object {
         $ListItem  = Get-PnPProperty -ClientObject $_ -Property FieldValuesAsText
         $ListRow = New-Object PSObject
         $Counter++
         ForEach($Field in $SelectedFields) 
         {
             $ListRow | Add-Member -MemberType NoteProperty $Field $ListItem[$Field]
         }
         Write-Progress -PercentComplete ($Counter / $($ListItems.Count)  * 100) -Activity "Exporting List Items..." -Status  "Exporting Item $Counter of $($ListItems.Count)"
         $ListDataCollection += $ListRow
 }
 #Export the result Array to CSV file
 $ListDataCollection | Export-CSV $CSVPath -NoTypeInformation

189545-1.jpg


189514-2.jpg


189562-3.jpg


Reference:

Thanks,
Echo Du
=========================================
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





1.jpg (69.0 KiB)
2.jpg (174.1 KiB)
3.jpg (81.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @venkateshpadmanabhan-5594 ,

Would you tell me whether your issue has been resolved or have any update?
I am looking forward to your reply.

Have a nice day!

Thanks,
Echo Du

0 Votes 0 ·

@venkateshpadmanabhan-5594 Have you tried Echo's answer? If you have any concerns about this thread, please feel free to reply.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

0 Votes 0 ·