SharePoint: How to get all the List Items in PowerShell through REST!

This time I’m sharing a Script that allows to get all the list elements in a SharePoint list making a REST call in PowerShell using the Invoke-RestMethod cmdlet. You can download the script from the following URL: How to get list items in a SharePoint list using REST

$host.Runspace.ThreadOptions = "ReuseThread"

 

#Definition of the function that makes the REST request

function Get-SPListsItemsUsingRESTAPI

{

    param ($sRESTUrl,$sUserName,$sPassword,$WebRMethod,$sListName,$hHeaders)

    try

    {    

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

        Write-Host "Getting all the list elements of $sListName using REST" -ForegroundColor Green

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

        $sSecPassword=ConvertTo-SecureString $sPassword -AsPlainText -Force        

        $spCredentials = New-Object System.Management.Automation.PSCredential($sUserName, $sSecPassword)

        $spRESTResults=Invoke-RestMethod -Uri $sRESTUrl -Credential $spCredentials -Method $WebRMethod -Headers $hHeaders

        $spRESTResultsCorrected = $spRESTResults  -creplace '"Id":','"Fake-Id":'

        $spResults = $spRESTResultsCorrected | ConvertFrom-Json

        $spListItems=$spResults.d.results

        Write-Host "Consultando los datos de la lista $sListName" -ForegroundColor Green

        foreach($spListItem in $spListItems){

            Write-Host "Name: " $spListItem.FirstName " " $spListItem.Title " - Company: " $spListItem.Company " - E-Mail: " $spListItem.Email -ForegroundColor Green

        }        

               

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()   

    }    

}

 

#Required Parameters

$sUserName = "<Dominio>\<Usuario>" 

$sPassword ="<Password>"

$WebRMethod=[Microsoft.PowerShell.Commands.WebRequestMethod]::Get

$sListName="Autores"

$hHeaders= @{

            "accept" = "application/json;odata=verbose"

            }

$sSiteUrl="http://<Url_Sitio>"

$sRESTUrl=$sSiteUrl + "/_api/lists/GetByTitle('" + $sListName + "')/items?$select=Title,FirstName,Company,Email"

#Calling the function

Get-SPListsItemsUsingRESTAPI -sRESTUrl $sRESTUrl -sListName $sListName -sUserName $sUserName -sPassword $sPassword -WebRMethod $WebRMethod -hHeaders $hHeader

And this is the output you will get when you execute the script:

image

SharePoint: Como obtener los elementos de una Lista en PowerShell mediante REST!

En esta ocasión os comparto un Script que permite obtener los elementos de una lista de SharePoint en PowerShell haciendo una llamada REST mediante el cmdlet Invoke-RestMethod. El script en cuestión os lo podéis descargar de: How to get list items in a SharePoint list using REST

$host.Runspace.ThreadOptions = "ReuseThread"

 

#Definition of the function that makes the REST request

function Get-SPListsItemsUsingRESTAPI

{

    param ($sRESTUrl,$sUserName,$sPassword,$WebRMethod,$sListName,$hHeaders)

    try

    {    

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

        Write-Host "Getting all the list elements of $sListName using REST" -ForegroundColor Green

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

        $sSecPassword=ConvertTo-SecureString $sPassword -AsPlainText -Force        

        $spCredentials = New-Object System.Management.Automation.PSCredential($sUserName, $sSecPassword)

        $spRESTResults=Invoke-RestMethod -Uri $sRESTUrl -Credential $spCredentials -Method $WebRMethod -Headers $hHeaders

        $spRESTResultsCorrected = $spRESTResults  -creplace '"Id":','"Fake-Id":'

        $spResults = $spRESTResultsCorrected | ConvertFrom-Json

        $spListItems=$spResults.d.results

        Write-Host "Consultando los datos de la lista $sListName" -ForegroundColor Green

        foreach($spListItem in $spListItems){

            Write-Host "Name: " $spListItem.FirstName " " $spListItem.Title " - Company: " $spListItem.Company " - E-Mail: " $spListItem.Email -ForegroundColor Green

        }        

               

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()   

    }    

}

 

#Required Parameters

$sUserName = "<Dominio>\<Usuario>" 

$sPassword ="<Password>"

$WebRMethod=[Microsoft.PowerShell.Commands.WebRequestMethod]::Get

$sListName="Autores"

$hHeaders= @{

            "accept" = "application/json;odata=verbose"

            }

$sSiteUrl="http://<Url_Sitio>"

$sRESTUrl=$sSiteUrl + "/_api/lists/GetByTitle('" + $sListName + "')/items?$select=Title,FirstName,Company,Email"

#Calling the function

Get-SPListsItemsUsingRESTAPI -sRESTUrl $sRESTUrl -sListName $sListName -sUserName $sUserName -sPassword $sPassword -WebRMethod $WebRMethod -hHeaders $hHeader

Y la salida por pantalla correspondiente es la siguiente:

image