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

Un pensamiento en “SharePoint: How to get all the List Items in PowerShell through REST!

  1. Pingback: SharePoint & Office 365: Resumen de posts (IV)! | Pasión por la tecnología...

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s